Hibernate Gurus,
Child has reference to parent. Parent has reference to the child. Only child is persisted.
In a typical parent/child (bi-directional with inverse=true etc.) relationship, and in a master-detail web-page design, we load up a list of Parent.Id/Parent.col_x/Parent.col... (for display only on the master page) in one Session (then the session is closed). Using parent.id to link, we open up the detail page to maintain the Child. We get child info from JSP and start a new hibernate session to do session.saveOrUpdate(child) (//this is commented out - session.saveOrUpdate(parent);) to add this new child to Oracle.
Parent.id is a non-nullable FK in the Child, so we have to do a child.setParent(new Parent().setId(1)) before doing session.save(child). Since parent with id=1 already loaded (and detached) in session one, the new Parent does not match with that detached parent, so we get something like -
net.sf.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: pacakgeName.Parent
Without change to the hbm mappings, now we have two workarounds
parent = session.load(parent, 1); //another trip to DB, bad
child.setParent(parent);
session.saveOrUpdate(child);
or
parent = getCachedParentFromList(1); //we do not want to keep a huge List in MEM
child.setParent(parent);
session.saveOrUpdate(child);
I was wondering if there is better approach other than these two workarounds?
Thanks in advance!
JohnZ
jzhao@amfam.com