Hibernate suppression d'une clé composée
bonjour
tout d'abbord voici une partie de mon architecture
classe Employe
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
package com.spe.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name="employer")
public class Employer implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private Long id_employer;
@Column(name="matricule")
private String matricule_employer;
@Column(name="nom_fr")
private String nom_employer_fr;
@Column(name="nom_fille_fr")
private String nom_fille_employer_fr;
@Column(name="prenom_fr")
private String prenom_employer_fr;
@Column(name="fonction_fr")
private String fonction_employer_fr;
@Column(name="datenaissance")
@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
private DateTime date_naissance ;
@Column(name="lieu_fr")
private String lieu_naissance_fr ;
@Column(name="daterecrutement")
@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
private DateTime date_recrutement ;
@Column(name="nom_ar")
private String nom_employer_ar;
@Column(name="nom_fille_ar")
private String nom_fille_employer_ar;
@Column(name="prenom_ar")
private String prenom_employer_ar;
@Column(name="fonction_ar")
private String fonction_employer_ar;
@Column(name="lieu_ar")
private String lieu_naissance_ar ;
@Column(name="sexe")
private String sexe_employer;
@Column(name="situation")
private String situation_employer;
@Column(name="actif")
private String actif_employer;
@OneToMany(mappedBy = "employer")
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
@OnDelete(action = OnDeleteAction.CASCADE)
private Set<EmployerFonction> fonctions = new HashSet<EmployerFonction>();
public Employer()
{
}
//id_employer
public Long getId() {
return id_employer;
}
public void setId( Long id_employer ) {
this.id_employer = id_employer;
}
//matricule_employer
public String getMatricule_employer() {
return matricule_employer;
}
public void setMatricule_employer( String matricule_employer ) {
this.matricule_employer = matricule_employer;
}
// nom_employer
public String getNom_employer_fr()
{
return nom_employer_fr;
}
public void setNom_employer_fr(String nom_employer_fr)
{
this.nom_employer_fr = nom_employer_fr;
}
// nom_fille_employer
public String getNom_fille_employer_fr()
{
return nom_fille_employer_fr;
}
public void setNom_fille_employer_fr(String nom_fille_employer_fr)
{
this.nom_fille_employer_fr = nom_fille_employer_fr;
}
// prenom_employer_fr
public String getPrenom_employer_fr()
{
return prenom_employer_fr;
}
public void setPrenom_employer_fr(String prenom_employer_fr)
{
this.prenom_employer_fr = prenom_employer_fr;
}
// fonction_employer_fr
public String getFonction_employer_fr()
{
return fonction_employer_fr;
}
public void setFonction_employer_fr(String fonction_employer_fr)
{
this.fonction_employer_fr = fonction_employer_fr;
}
//date_naissance
public DateTime getDate_naissance()
{
return date_naissance;
}
public void setDate_naissance(DateTime date_naissance)
{
this.date_naissance = date_naissance;
}
// lieu_naissance
public String getLieu_naissance_fr()
{
return lieu_naissance_fr;
}
public void setLieu_naissance_fr(String lieu_naissance_fr)
{
this.lieu_naissance_fr = lieu_naissance_fr;
}
//date_recrutement
public DateTime getDate_recrutement()
{
return date_recrutement;
}
public void setDate_recrutement(DateTime date_recrutement)
{
this.date_recrutement = date_recrutement;
}
// nom_employer
public String getNom_employer_ar()
{
return nom_employer_ar;
}
public void setNom_employer_ar(String nom_employer_ar)
{
this.nom_employer_ar = nom_employer_ar;
}
// nom_fille_employer_ar
public String getNom_fille_employer_ar()
{
return nom_fille_employer_ar;
}
public void setNom_fille_employer_ar(String nom_fille_employer_ar)
{
this.nom_fille_employer_ar = nom_fille_employer_ar;
}
// prenom_employer
public String getPrenom_employer_ar()
{
return prenom_employer_ar;
}
public void setPrenom_employer_ar(String prenom_employer_ar)
{
this.prenom_employer_ar = prenom_employer_ar;
}
// fonction_employer_ar
public String getFonction_employer_ar()
{
return fonction_employer_ar;
}
public void setFonction_employer_ar(String fonction_employer_ar)
{
this.fonction_employer_ar = fonction_employer_ar;
}
// lieu_naissance
public String getLieu_naissance_ar()
{
return lieu_naissance_ar;
}
public void setLieu_naissance_ar(String lieu_naissance_ar)
{
this.lieu_naissance_ar = lieu_naissance_ar;
}
// sexe_employer
public String getSexe_employer()
{
return sexe_employer;
}
public void setSexe_employer(String sexe_employer)
{
this.sexe_employer = sexe_employer;
}
// situation_employer
public String getSituation_employer()
{
return situation_employer;
}
public void setSituation_employer(String situation_employer)
{
this.situation_employer = situation_employer;
}
// actif_employer
public String getActif_employer()
{
return actif_employer;
}
public void setActif_employer(String actif_employer)
{
this.actif_employer = actif_employer;
}
// fonctions
public Set<EmployerFonction> getFonctions()
{
return fonctions;
}
public void setFonctions(Set<EmployerFonction> fonctions)
{
this.fonctions = fonctions;
}
} |
classe Fonction
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
package com.spe.entity;
import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name="fonction")
public class Fonction {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private Long id_fonction;
@Column(name="code_poste")
private String code_poste;
@Column(name="nom_fr")
private String nom_fonction_fr;
@Column(name="nom_ar")
private String nom_fonction_ar;
/*
@OneToOne // la relation one to one entre fonction et famille_fonction (a revoir one to one ne ma pas convincu )
@JoinColumn(
name="id_famille", // c le nom de la colonne bdd de la table Fonction
referencedColumnName="id" // c le nom de lla colonne bdd de la table Famille
)
private Famille famille; // la classe qu'on fait avec la relation
*/
@ManyToOne(
cascade={javax.persistence.CascadeType.PERSIST, javax.persistence.CascadeType.MERGE, javax.persistence.CascadeType.REFRESH }
)
@JoinColumn(name="id_famille") // c le nom de la colonne bdd de la table Fonction
private Famille famille; // la classe qu'on fait avec la relation
// relaiton avec EmployerFonction na pas de correspondance ds la BDD c juste pour relation biderectionnel avec lentity E3mployerFonctyion
@OneToMany(mappedBy = "fonction")
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
@OnDelete(action = OnDeleteAction.CASCADE)
private Set<EmployerFonction> employers = new HashSet<EmployerFonction>();
public Fonction() {
}
//id_fonction
public Long getId() {
return id_fonction;
}
public void setId( Long id_fonction ) {
this.id_fonction = id_fonction;
}
// code_poste
public String getCode_poste()
{
return code_poste;
}
public void setCode_poste(String code_poste)
{
this.code_poste = code_poste;
}
// nom_fonction_fr
public String getNom_fonction_fr()
{
return nom_fonction_fr;
}
public void setNom_fonction_fr(String nom_fonction_fr)
{
this.nom_fonction_fr = nom_fonction_fr;
}
// nom_fonction_ar
public String getNom_fonction_ar()
{
return nom_fonction_ar;
}
public void setNom_fonction_ar(String nom_fonction_ar)
{
this.nom_fonction_ar = nom_fonction_ar;
}
// acl_fonction
public Famille getFamille()
{
return famille;
}
public void setFamille(Famille famille)
{
this.famille = famille;
}
// employers
public Set<EmployerFonction> getEmployers()
{
return employers;
}
public void setEmployers(Set<EmployerFonction> employers)
{
this.employers = employers;
}
} |
classe EmployerFonction
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
package com.spe.entity;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
import java.io.Serializable;
import java.sql.Timestamp;
@Entity
@Table(name="employer_fonction")
public class EmployerFonction implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@EmbeddedId
private EmployeFonctionId id;
@MapsId("employeId")
@ManyToOne
@JoinColumn(name = "id_employer")
private Employer employer;
@MapsId("conceptId")
@ManyToOne
@JoinColumn(name = "id_fonction")
private Fonction fonction;
@Column(name="date_debut")
@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
private DateTime date_debut ;
@Column(name="date_fin")
@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
private DateTime date_fin;
public EmployerFonction()
{
}
public EmployerFonction(Employer employer, Fonction fonction, DateTime date_debut, DateTime date_fin) {
this.employer = employer;
this.fonction = fonction;
employer.getFonctions().add(this);
fonction.getEmployers().add(this);
this.id = new EmployeFonctionId(fonction.getId(), employer.getId());
this.date_debut = date_debut;
this.date_fin = date_fin;
}
//date_debut
public DateTime getDate_debut()
{
return date_debut;
}
public void setDate_debut(DateTime date_debut)
{
this.date_debut = date_debut;
}
//date_fin
public DateTime getDate_fin()
{
return date_fin;
}
public void setDate_fin(DateTime date_fin)
{
this.date_fin = date_fin;
}
} |
classe EmployeFonctionID
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
package com.spe.entity;
import java.io.Serializable;
import javax.persistence.Embeddable;
@Embeddable
public class EmployeFonctionId implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Long employeId;
private Long fonctionId;
public EmployeFonctionId() {
}
public EmployeFonctionId(Long employeId, Long fonctionId) {
this.employeId = employeId;
this.fonctionId = fonctionId;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
EmployeFonctionId other = (EmployeFonctionId) obj;
if (employeId == null) {
if (other.employeId != null)
return false;
} else if (!employeId.equals(other.employeId))
return false;
if (fonctionId == null) {
if (other.fonctionId != null)
return false;
} else if (!fonctionId.equals(other.fonctionId))
return false;
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((employeId == null) ? 0 : employeId.hashCode());
result = prime * result
+ ((fonctionId == null) ? 0 : fonctionId.hashCode());
return result;
}
} |
le probléme c'est qu'on je fait
Code:
1 2
|
employerDao.supprimer( employer ); |
j'ai l'erreur suivante
[http-bio-8080-exec-10] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1451, SQLState: 23000
[http-bio-8080-exec-10] ERROR org.hibernate.util.JDBCExceptionReporter - Cannot delete or update a parent row: a foreign key constraint fails (`db_speppo_albayan`.`employer_fonction`, CONSTRAINT `FK26B4BD5624B629B2` FOREIGN KEY (`id_employer`) REFERENCES `employer` (`id`))
[http-bio-8080-exec-10] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
je sais que le probleme est du au foreginKey et j'ai utiliser les CASCADE lors de la suppression mais toujours rien