hibernate - Spring Data JPA - ManyToOne unable to delete child without modifying parent list -
i struggle week following problem:
how possible delete child entity through repository without modifying list on owning (parent) side of relation?
thanks in advance.
i hoping answers!
the child class:
@entity @table(name = "child") @cache(usage = cacheconcurrencystrategy.nonstrict_read_write) public class child implements serializable {      private static final long serialversionuid = 1l;      @id     @generatedvalue(strategy = generationtype.sequence, generator = "sequencegenerator")     @sequencegenerator(name = "sequencegenerator")     private long id;      @manytoone     private parent parent;      public long getid() {         return id;     }      public void setid(long id) {         this.id = id;     }      public long getparent() {         return parent;     }      public void setparent(parent parent) {         this.parent = parent;     } } and parent class:
@entity @table(name = "parent") @cache(usage = cacheconcurrencystrategy.nonstrict_read_write) public class parent implements serializable {      private static final long serialversionuid = 1l;      @id     @generatedvalue(strategy = generationtype.sequence, generator = "sequencegenerator")     @sequencegenerator(name = "sequencegenerator")     private long id;      @onetomany(mappedby = "parent", cascade = cascadetype.all, orphanremoval = true)     @jsonignore     @cache(usage = cacheconcurrencystrategy.nonstrict_read_write)     private set<child> children = new hashset<>();      public long getid() {         return id;     }      public void setid(long id) {         this.id = id;     }      public set<child> getchildren() {         return children;     }      public void setchildren(set<child> children) {          this.children = children;     }      public parent addchild(child child) {         this.children.add(child);         child.setparent(this);         return this;     }      public parent removechild(child child) {         this.children.remove(child);         child.setparent(null);         return this;     } } and here test:
@test @transactional public void testparenttochildrelationship() {     parent parent = new parent();     child child = new child();      parent.addchild(child);     parent.addchild(new child());     parent.addchild(new child());     parent.addchild(new child());      parentrepository.save(parent);      assertions.assertthat(parentrepository.count()).isequalto(1l);     assertions.assertthat(childrepository.count()).isequalto(4l);      childrepository.delete(child);       assertions.assertthat(parentrepository.count()).isequalto(1l);     // fails     assertions.assertthat(childrepository.count()).isequalto(3l);      parentrepository.delete(parent.getid());      assertions.assertthat(parentrepository.count()).isequalto(0l);     assertions.assertthat(childrepository.count()).isequalto(0l); } the test work if insert before deleting child,
child.getparent().removechild(child); but want avoid calling this. there way make work calling child-jpa-repository.delete method? or other annotations missed?
since child has association parent facing issue, need remove link between child , parent either using
parent.removechild(child); or
child.getparent().removechild(child); 
Comments
Post a Comment