java - Get the child id when persisting an entity with cascade -


i'm using hibernate entity manager (4.2.16). i'm having trouble when merging existing entity, after adding new child it. id of newly created child, id not set. here model:

@entity @table(name = "parent") @genericgenerator(name = "gen_identifier", strategy = "sequence", parameters = {     @parameter(name = "sequence", value = "sq_parent") }) public class parent {      @id     @generatedvalue(strategy = generationtype.sequence, generator = "gen_identifier")     private long id;      @onetomany(cascade = cascadetype.all, orphanremoval = true, mappedby = "parent")     private set<child> children; }  @entity @table(name = "child") @genericgenerator(name = "gen_identifier", strategy = "sequence", parameters = {     @parameter(name = "sequence", value = "sq_child") }) public class child {      @id     @generatedvalue(strategy = generationtype.sequence, generator = "gen_identifier")     private long id;      @manytoone(fetch = fetchtype.lazy)     @joincolumn(name = "parent_id")     private parent parent; } 

code create parent (transaction 1):

public long saveparent() {     parent parent = new parent();      entitymanager.persist(parent);      system.out.println("saveparent : parent.id = " + parent.getid());     return parent.getid(); } 

code add child (transaction 2)

public void addchild(parent parent) {     child child = new child();     child.setparent(parent);     parent.getchildren().add(child);      entitymanager.merge(parent);     system.out.println("addchild : parent.id = " + parent.getid());      // following give me null id     system.out.println("addchild : child.id = " + parent.getchildren().iterator().next().getid());      system.out.println("addchild : child.id = " + child.getid()); } 

when executing code, expect child id not null. here output :

saveparent : parent.id = 1000 addchild : parent.id = 1000 addchild : child.id = null addchild : child.id = null 

merge returns newly intiallized entity should use entity operation changes happens after call merge reflected in entity returned merge.so in order solve problem need change code below

public parent addchild(parent parent) { child child = new child(); child.setparent(parent); parent.getchildren().add(child);  parent=entitymanager.merge(parent); system.out.println("addchild : parent.id = " + parent.getid());  // following give me null id system.out.println("addchild : child.id = " + parent.getchildren().iterator().next().getid());  system.out.println("addchild : child.id = " + child.getid());' return parent; 

}

you should return parent method code calling method should use newly generated parent rather using old 1 not have changes done merge in it.

to read more merge() visit below link

http://blog.xebia.com/jpa-implementation-patterns-saving-detached-entities/


Comments

Popular posts from this blog

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

matlab - error with cyclic autocorrelation function -

php - Using grpc in Laravel, "Class 'Grpc\ChannelCredentials' not found." -