java - Help understanding the hibernate behavior/error -


i have product class has 1-to-many association (composite pattern). below pojo, hbm, test code , log/error respectively. can explain error about. why getting error when setting parent's primary key?

product pojo

public class product {     private long id;      private set<product> children = collections.emptyset();      public void addchild(final product child)     {         if (children.isempty())         {             children = sets.newhashset();         }         child.setparent(this);         children.add(child);     } } 

hbm.xml

<class name="product" table="product">         <set name="children" lazy="false" table="product">             <key>                 <column name="id" />             </key>             <one-to-many class="product" />         </set> </class> 

test

public void save()     {         // level 1 - mortgage lob         product mortgage = new product();         mortgage.setcode("mortgage");          product ml = new product();         ml.setcode("mortgage loan");          product me = new product();         me.setcode("home equity loc");          //level 2         mortgage.addchild(ml);         mortgage.addchild(me);          hibernatetemplate.save(mortgage);     } 

log , error

debug [org.hibernate.sql] insert product (id, code, name, startdate, enddate, isdecisionable, isselectable) values (null, ?, ?, ?, ?, ?, ?) debug [org.hibernate.type.stringtype] binding 'mortgage' parameter: 1 debug [org.hibernate.type.stringtype] binding null parameter: 2 debug [org.hibernate.type.timestamptype] binding null parameter: 3 debug [org.hibernate.type.timestamptype] binding null parameter: 4 debug [org.hibernate.type.booleantype] binding 'false' parameter: 5 debug [org.hibernate.type.booleantype] binding 'false' parameter: 6 debug [org.hibernate.sql] call identity() debug [org.hibernate.sql] update product set id=? id=? debug [org.hibernate.type.longtype] binding '1' parameter: 1 error [org.hibernate.event.def.abstractflushingeventlistener] not synchronize database state session org.hibernate.transientobjectexception: object references unsaved transient instance - save transient instance before flushing: com.product     @ org.hibernate.engine.foreignkeys.getentityidentifierifnotunsaved(foreignkeys.java:219)     @ org.hibernate.type.entitytype.getidentifier(entitytype.java:397)     @ org.hibernate.type.manytoonetype.nullsafeset(manytoonetype.java:78) 

a transient object 1 has not been saved. mapped, relationship not use 'transitive persistence', say, have not told hibernate when save parent want save children. guess error because have instances of children (doesn't matter children have same class of parent) on parent (in session), , hibernate aware of that, doesn't know them.

you can either:

1) save children before saving parent
2) add 'cascade' attribute mapping, cascade="save" or cascade="all"

see http://docs.jboss.org/hibernate/core/3.3/reference/en/html/objectstate.html#objectstate-transitive


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -