These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Session merge,update, parent child relationship
PostPosted: Thu Dec 08, 2005 5:46 pm 
Beginner
Beginner

Joined: Wed Jul 06, 2005 4:51 pm
Posts: 27
I am trying to model a parent child relationship with a one-to-many and cascade="all" option. I didn't go for bidirectional relationship because I always traverse from parent to child.

The persistent objects from the server are retrieved and passed onto gui where they are manipulated and sent back to server. Thus the objects I deal with in the update are transient/detached.

I tried both update and merge methods and I encountered different problems with both.

1. With update

When GUI removes some of the children and adds totally new children to the parent and passes it on to the server, the removed children from gui are not getting deleted in the database. Newly added children are getting appended ( with appropriate ids generated), any updates to child's data is getting updated, only the removed children are not being deleted from the database. As the parent/children being persisted was detached, hibernate would clearly not know that children have been removed I guess. So I thought merge would be better as it is going to merge the detached state onto database.

2. With merge

Merge works nicely and solves the above problem of deleting the gui removed children from the database but on the other hand if gui adds new children ( which wont have the ids - as they are being generated by hilo ), hibernate spits out the below error. I was expecting a nice merge where newly added children by gui would be added to the database, removed children by gui would be deleted from database and updated children by gui would be updated in database. The last two do happen but not the first one. Any suggestions on what I should use?


Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: test.Child._ChildBackref
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:236)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:95)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:157)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:104)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:544)
at org.hibernate.engine.Cascades$6.cascade(Cascades.java:176)
at org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:771)
at org.hibernate.engine.Cascades.cascade(Cascades.java:720)
at org.hibernate.engine.Cascades.cascadeCollection(Cascades.java:895)
at org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:792)
at org.hibernate.engine.Cascades.cascade(Cascades.java:720)
at org.hibernate.engine.Cascades.cascade(Cascades.java:847)
at org.hibernate.event.def.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:264)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:223)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:102)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:54)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:539)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 09, 2005 1:41 am 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
Your first approach should work. Setting cascade="all-delete-orphan" tells Hibernate to delete those children that aren't referenced any more. Make sure that all references to them from all objects known to Hibernate are cut before issuing update on the parent.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 09, 2005 11:10 am 
Beginner
Beginner

Joined: Wed Jul 06, 2005 4:51 pm
Posts: 27
I was using cascade="all-delete-orphan" ( forgot to mention that in the original post ), but the children were not being deleted. The objects that I am trying pass to session.update are detached ( they do not have any hibernate related collection implementations )


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 09, 2005 11:48 am 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
Quote:
I am trying to model a parent child relationship with a one-to-many and cascade="all" option.


Quote:
The objects that I am trying pass to session.update are detached ( they do not have any hibernate related collection implementations )


If you are talking about the same objects in both quotes, then they do have a hibernate related collection implementation, namely the one-to-many with cascade="all".

Being detached means they were made known to Hibernate in a previous session that has since then been ended. To attach them again, either release them from memory after the first session and retrieve them again from the database in the second session; or re-attach them in the second session before using them in your application by issuing a lock() command on them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 09, 2005 12:17 pm 
Beginner
Beginner

Joined: Wed Jul 06, 2005 4:51 pm
Posts: 27
Let me elaborate my implemenation

Once I retrieve objects from hibernate, I deep clone them but replace hibernate related collection implementations with regular java collections to stream them out to GUI. Gui modifies the parent/children and send them back to the server. This new parent/children objects is what I am trying to persist. I use totally different session for this operation and I tried both session.update and session.merge as I mentioned in the original post. I have cascade="all-delete-orphan"

With merge, I see that newly added children throw the exception I posted before - unreferenced children are nicely removed.

With update, the unrefernced children are not being removed from database, newly added children are nicely added to the database.

The parent/children manipulation is happening on objects that are outside the session and without any hibernate collection implementations.

Please let me know if my post is not clear.

I will look at the lock method now.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 09, 2005 3:00 pm 
Beginner
Beginner

Joined: Wed Jul 06, 2005 4:51 pm
Posts: 27
I believe I got the anwer

Check this Jira

Cascade merge() and unidirectional one-to-many
http://opensource2.atlassian.com/projec ... se/HHH-749


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.