java - Hibernate can not correctly operates on multiple bags -


i have model has 2 onetomany relation 2 model. when remove 1 record first bag , save object, hibernate removes 1 record 1st bag ok removes record has same index record in first bag 2nd bag. can not figure out how fix it.

here models

owner:

@entity @table(name = "table_a") public class owner extends serializable {   private static final long serialversionuid = 1l;   @id  @column(name = "id", unique = true, nullable = false)  private int id;   @onetomany(mappedby = "owner", fetch = fetchtype.eager, orphanremoval = true, cascade = cascadetype.all)  @ordercolumn(name = "position")  private list<dog> dogs;   @onetomany(mappedby = "owner", fetch = fetchtype.eager, orphanremoval = true, cascade = cascadetype.all)  @ordercolumn(name = "position")  private list<cat> cats;   public long getid() {     this.id;       }   public void setid(long id) {     this.id = id;  }   public dog getdogs() {     return this.dogs;  }   public void setdogs(list<dog> dogs) {     this.dogs = dogs;  }   public void adddog(dog dog) {     dog.owner = this;     this.dogs.add(dog);  }   public void removedog(dog dog) {     this.dogs.remove(dog);  }    public dog getcats() {     return this.cats;  }   public void setcats(list<cat> cats) {     this.cats = cats;  }   public void addcat(cat cat) {     cat.owner = this;     this.cats.add(cat);  }   public void removecat(cat cat) {     this.cats.remove(cat);  } }    

dog:

@entity @table(name = "table_b") public class dog extends serializable {   private static final long serialversionuid = 1l;   @id  @column(name = "id", unique = true, nullable = false)  private int id;   @manytoone  @joincolumn(name = "owner_id")  private owner owner;   @column(name = "position")  private int position;   public long getid() {     return this.id;  }   public void setid(long id) {     this.id = id;  }   public int getposition() {     return this.position;  }   public void setposition(int index) {     this.position = index;  }   public owner getowner() {     return this.owner;  }   public void setowner(owner owner) {     this.owner = owner;  } }  

cat:

@entity @table(name = "table_c") public class cat extends serializable {   private static final long serialversionuid = 1l;   @id  @column(name = "id", unique = true, nullable = false)  private int id;   @manytoone  @joincolumn(name = "owner_id")  private owner owner;   @column(name = "position")  private int position;   public long getid() {     return this.id;  }   public void setid(long id) {     this.id = id;  }   public int getposition() {     return this.position;  }   public void setposition(int index) {     this.position = index;  }   public owner getowner() {     return this.owner;  }   public void setowner(owner owner) {     this.owner = owner;  } }    

let had owner has 2 cats , 2 dogs.

when that:

cat cat2remove = owner.getcats().get(1); owner.removecat(cat2remove); session.save(owner); 

hibernate removes 2nd cat table_b intend removes 2nd dog table_c , wonder how can prevent in proper way?

this happens due cascade = cascadetype.all have remove cascadetype.all understand effect of cascade see this link.

if want cascade on save-update use cascade="save-update" don't know if need orphanremoval = true in case or not @ these answers see if need in business or not.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -