Probleme avec suppression en cascade
Bonjour,
Alors j'ai mit en oeuvre les annotations de type cascade pour supprimer en cascade tout ce qui est relié, dans mon cas, a la table "Person".
La suppression en cascade marche sauf pour deux relations avec deux autres tables qui sont toutes les deux de types @ManyToOne et @OneToMany
Meme en mettant du @OneToOne ca ne marchait pas donc voila je dois probablement faire une erreur, bien que c'est etrangre vu qu'avec le reste ca fonctionne.
Voici le code de mon entité "Person" :
Person.java :
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
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jim.sums.db;
import javax.persistence.CascadeType;
import jim.common.jimaas.struts2.User;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
/**
*
* @author BreyA
*/
@Entity
public class Person implements Serializable, User {
private static final long serialVersionUID = 1L;
private Long id;
private String forename;
private String surname;
private String username;
private String password;
private Timestamp creationDate;
private Timestamp confirmationDate;
private Timestamp lastLogin;
private Set<Cohort> cohort = new HashSet<Cohort>();
private Set<Email> email = new HashSet<Email>();
private Organisation organisation;
private PersonStatus status;
private Set<Phone> phone = new HashSet<Phone>();
private StudentIdentifier studentIdentifier;
private Set<Suspension> suspended = new HashSet<Suspension>();
private Set<Suspension> instigater = new HashSet<Suspension>();
private Set<Cohort> cohortCoordinating = new HashSet<Cohort>();
@OneToMany(mappedBy = "personCoordinating")
public Set<Cohort> getCohortCoordinating() {
return cohortCoordinating;
}
public void setCohortCoordinating(Set<Cohort> cohortCoordinating) {
this.cohortCoordinating = cohortCoordinating;
}
public void setCohort(Set<Cohort> cohort) {
this.cohort = cohort;
}
@ManyToMany
public Set<Cohort> getCohort() {
return cohort;
}
public void setInstigater(Set<Suspension> instigaterPerson) {
this.instigater = instigaterPerson;
}
public void setSuspended(Set<Suspension> suspendedPerson) {
this.suspended = suspendedPerson;
}
@OneToMany(mappedBy = "instigaterPerson", cascade = {CascadeType.REMOVE})
public Set<Suspension> getInstigater() {
return instigater;
}
@ManyToMany(mappedBy = "suspendedPerson", cascade = {CascadeType.REMOVE})
public Set<Suspension> getSuspended() {
return suspended;
}
@OneToOne
public StudentIdentifier getStudentIdentifier() {
return studentIdentifier;
}
public void setStudentIdentifier(StudentIdentifier studentIdentifier) {
this.studentIdentifier = studentIdentifier;
}
@ManyToMany(mappedBy = "personPhone", cascade = {CascadeType.REMOVE})
public Set<Phone> getPhone() {
return phone;
}
public void setPhone(Set<Phone> phone) {
this.phone = phone;
}
@ManyToOne
public PersonStatus getStatus() {
return status;
}
public void setStatus(PersonStatus personStatus) {
this.status = personStatus;
}
@ManyToOne
public Organisation getOrganisation() {
return organisation;
}
public void setOrganisation(Organisation organisation) {
this.organisation = organisation;
}
@OneToMany(mappedBy = "personEmail", cascade = {CascadeType.REMOVE})
public Set<Email> getEmail() {
return email;
}
public void setEmail(Set<Email> email) {
this.email = email;
}
public void setConfirmationDate(Timestamp confirmationDate) {
this.confirmationDate = confirmationDate;
}
public void setCreationDate(Timestamp creationDate) {
this.creationDate = creationDate;
}
public void setLastLogin(Timestamp lastLogin) {
this.lastLogin = lastLogin;
}
public Timestamp getConfirmationDate() {
return confirmationDate;
}
public Timestamp getCreationDate() {
return creationDate;
}
public Timestamp getLastLogin() {
return lastLogin;
}
@Override
public void setForename(String forename) {
this.forename = forename;
}
@Override
public void setPassword(String password) {
this.password = password;
}
@Override
public void setSurname(String surname) {
this.surname = surname;
}
@Override
public void setUsername(String username) {
this.username = username;
}
@Column
@Override
public String getForename() {
return forename;
}
@Column
@Override
public String getPassword() {
return password;
}
@Column
@Override
public String getSurname() {
return surname;
}
@Column
@Override
public String getUsername() {
return username;
}
@Override
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Override
public Long getId() {
return id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Person)) {
return false;
}
Person other = (Person) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "jim.sums.db.Person[id=" + id + "]";
}
@Override
public boolean isUserInRole(String role) {
throw new UnsupportedOperationException("Not supported yet.");
}
} |
Organisation.java :
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
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jim.sums.db;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
/**
*
* @author BreyA
*/
@Entity
public class Organisation implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String postalAddress;
private String postcode;
private String activity;
private Set<Person> personOrganisation = new HashSet<Person>();
@OneToMany(mappedBy = "organisation", cascade = {CascadeType.REMOVE})
public Set<Person> getPersonOrganisation() {
return personOrganisation;
}
public void setPersonOrganisation(Set<Person> personOrganisation) {
this.personOrganisation = personOrganisation;
}
public void setActivity(String activity) {
this.activity = activity;
}
public void setName(String name) {
this.name = name;
}
public void setPostalAddress(String postalAddress) {
this.postalAddress = postalAddress;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
@Column
public String getActivity() {
return activity;
}
@Column
public String getName() {
return name;
}
@Column
public String getPostalAddress() {
return postalAddress;
}
@Column
public String getPostcode() {
return postcode;
}
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Organisation)) {
return false;
}
Organisation other = (Organisation) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "jim.sums.db.Organisation[id=" + id + "]";
}
} |
StudentIdentifier.java :
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
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jim.sums.db;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
/**
*
* @author BreyA
*/
@Entity
public class StudentIdentifier implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String hemis;
private Person personStudent;
@OneToOne(mappedBy="studentIdentifier", cascade={CascadeType.REMOVE})
public Person getPersonStudent() {
return personStudent;
}
public void setPersonStudent(Person person) {
this.personStudent = person;
}
public void setHemis(String hemis) {
this.hemis = hemis;
}
@Column
public String getHemis() {
return hemis;
}
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof StudentIdentifier)) {
return false;
}
StudentIdentifier other = (StudentIdentifier) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "jim.sums.db.StudentNumber[id=" + id + "]";
}
} |
La suppression en cascade fonctionne pour tous sauf pour "Organisation" et "StudentIdentifier".
StudentIdentifier est de type @OneToOne
Organisation est de type @ManyToOne (dans Person) et @OneToMany (dans Organisation)
Donc quand je supprime une personne, son id d'Organisation ou de StudentIdentifier (c'est un ou l'autre) est supprimé et la pas de suppression en cascade.
Voila, j'espere que vous avez compris mon probleme.
Merci de m'aider ;)