salut
g 2 relations pere fils bidirectionnelle entre un projet et ses lots, entre un lot et ses taches,...
Projet 1-----* Lot 1 -----* Tache
mon pb vient que lorsque je supprime un lot, j'ai l'erreur comme quoi il est encore référencé qq part:
Quote:
org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [model.bo.Lot#114]
Pourtant, je supprime d'abord le lot de la liste des lots du projet, puis je supprime le lot. De plus, j'ai mis un cascade="all-delete-orphan" pour supprimer par la meme occasion toutes les taches du lot...
Quand je supprime le projet, ca supprime tout bien!
mapping de projet:
Quote:
<class name="Projet" table="PROJET" >
<id name="id" type="integer" column="idProjet" >
<generator class="native"/>
</id>
...
<set name="lots" lazy="true" cascade="all-delete-orphan" fetch="join" inverse="true" >
<key column="refProjet"/>
<one-to-many class="Lot"/>
</set>
...
mapping de lot:
Quote:
<class name="Lot" table="LOT" >
<id name="id" type="integer" column="idLot" >
<generator class="native"/>
</id>
<natural-id mutable="true" >
<property name="intitule" column="intitule" type="string" not-null="true" length="30" />
<many-to-one name="projet" column="refProjet" class="Projet" not-null="true" />
</natural-id>
...
<set name="taches" lazy="true" cascade="all-delete-orphan" fetch="join" inverse="true">
<key column="refLot" />
<one-to-many class="Tache"/>
</set>
Un projet:
Code:
...
Set<Lot> lots;
...
public java.util.Set<Lot> getLots () {
return lots;
}
public void setLots (java.util.Set<Lot> lots) {
this.lots = lots;
}
public void addTolots (Lot lot) {
if (null == getLots()) setLots(new java.util.HashSet<Lot>());
getLots().add(lot); // ajout dans la collection
lot.setProjet(this); // suppression de la collection
}
public boolean removeTolots (Lot lot) {
if (null != getLots()) {
if ( getLots().remove(lot) ){ // suppression de la collection
lot.setProjet(null); // suppresion du lien
return true;
}
}
return false;
}
Un lot:
Code:
Projet projet;
public Projet getProjet () {
return projet;
}
public void setProjet (Projet projet) {
this.projet = projet;
}
...
public boolean equals (Object obj) {
System.out.println("public boolean equals (Object obj)" + this.getId() );
if (null == obj) {
System.out.println("null == obj");
return false;
}
if (!(obj instanceof Lot)) {
System.out.println("!(obj instanceof Lot");
return false;
}
else {
Lot lot = (Lot) obj;
if (null == this.getId() || null == lot.getId()) {
System.out.println("LOT.equal: un des id est nul!" );
return false;
}
else {
System.out.println("this.getId()" + this.getId());
System.out.println("lot.getId()" + lot.getId());
System.out.println("LOT.equel: resultat = " + this.getId().equals(lot.getId()));
return (this.getId().equals(lot.getId()));
}
}
}
public int hashCode () {
System.out.println("LOT.hashCode()" + this.getId() );
if (null == this.getId()) return Integer.MIN_VALUE;
return this.getId();
}
public int compareTo (Object obj) {
System.out.println("LOT.compareTo");
if (obj.hashCode() > hashCode()) return 1;
else if (obj.hashCode() < hashCode()) return -1;
else return 0;
}
Mon service qui manipule les Objets et leur dao
Code:
public Lot creerLot(String intitule, int idProjet,
Calendar dateDebut, Calendar dateFin ){
Projet projet = projetDAO.findById(idProjet);
if (projet != null ){
try{
log.debug("Creation Lot rattache au projet #" + projet.getId() + " ...");
Lot lot = new Lot(intitule, projet, dateDebut, dateFin );
lot.setDateCreation( new GregorianCalendar());
projet.addTolots(lot);
lot = lotDAO.create(lot);
//projetDAO.update(projet);
return lot;
}
catch(Exception e){
log.warn("Pb de creation du lot...\n" + e.getMessage() );
}
}
else{System.out.println("Le projet auquel est rattache le lot n'existe pas"); } // Le projet auquel est rattache le lot n'existe pas
return null;
}
public void deleteLot(int idLot){
System.out.println("Supp du lot " + idLot);
Lot l = lotDAO.findById(idLot);
if ( l != null ){
Projet p = projetDAO.findById(l.getProjet().getId() );
if (p != null ){
System.out.println("LIST PROJET contient le lot?: " + p.getLots().contains(l) );
for ( Lot lx : p.getLots() ){
System.out.println("Projet yyyy --> " + lx );
}
System.out.println("SUPPRESSION DANS PROJET: " + p.removeTolots(l) );
System.out.println("SUPPRESSION DANS PROJET: " + p.removeTolots(l) );
projetDAO.update(p);
for ( Lot lx : p.getLots() ){
System.out.println("Projet yyyy --> " + lx );
}
//projetDAO.update(p);
lotDAO.delete(l);
}
else{
System.out.println("PROJET INTROUVABLE////:::::!!!!!!");
}
}
else{
System.out.println("LOT INTROUVABLE////:::::!!!!!!");
}
}
Un TestCase qui ouvre la session/transaction durant toute la durée du test
Code:
Projet p1 = projetManager.creerProjet(
"testP1",
new GregorianCalendar(),
dateDebP, dateFinP,
new Client(1) );
assertNotNull("projet1 null", p1.getId() );
...
Lot lot1 = projetManager.creerLot("lot1", p1.getId() , dateDebP, DateUtil.rollNewDate(dateDebP, Calendar.MONTH, 3) );
System.out.println("lot1 cree!");
assertNotNull("lot1 null", lot1.getId() );
Tache tache1 = projetManager.creerTache("tache1", lot1.getId(), 12., 0., 50.,
dateDebP, DateUtil.rollNewDate(dateDebP, Calendar.MONTH, 2)) ;
System.out.println("tache1 cree!");
assertNotNull("tache1 null", tache1.getId() );
Tache tache2 = projetManager.creerTache("tache2", lot1.getId(), 5., 0., 50.,
DateUtil.rollNewDate(dateDebP, Calendar.MONTH, 1), DateUtil.rollNewDate(dateDebP, Calendar.MONTH, 3)) ;
...
// SUPPRESSIONS
projetManager.deleteLot(lot1.getId());
En debuguant, j'ai remarqué que lorsque je fais p.getLots().contains(l), il retourne faut
deplus, il appel bien le hashcode() de Lot
Pourtant le lot que je cherche et le lot contenu dans la collection ont le meme ID, et ces méthodes se basent bien sur la comparaison des ID!
donc le p.getLots.remove(lot) ne marche pas!
Voyez vous le probleme?