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.  [ 4 posts ] 
Author Message
 Post subject: Does Hibernate keep an internal structure of objects?
PostPosted: Mon Nov 03, 2008 6:01 pm 
Beginner
Beginner

Joined: Wed Nov 08, 2006 8:24 am
Posts: 26
Location: Gothenburg, Sweden
I'm having a database with a parent-child-like structure. At some point I collect a list (using bag) of parents, and all parents have a list of children (also using bags) respectively. Also worth noting is that I keep the session open, in order to use lazy loading.

It is now possible for my users (it is a windows forms application) to add a child to a parent. The user can then edit the child, or save the child (plus some more actions...). The user can also delete the newly created child, i.e. delete the child from the parents list of children, before it is saved in the database. It is here I run into a major problem.

It seems like Hibernate has private version of the newly created child object, and when I delete the parents version of the child, the Hibernate version remains alive. If a add a new child to the parent and save it, Hibernate throws an exception when it tries to first delete the “old” child object, the object that was never meant to be saved.

I have tried to delete the old child object from the secondary cache (using the evict method), but since it has never been saved to, or loaded from, the database, the object is not located in the cache.

Does anyone have an idea of how I can get rid of the object?
Or is this simply how Hibernate works?

I would guess that if I close and reopen the session this problem would bot exist, but that is not an options for me. The session must remain open.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2008 2:39 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I suppose you save the newly created object (session.Save()) ? If the user than removes that child again, you have to evict it from the session (session.Delete() only applies to already persisted objects): session.Evict().

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2008 3:15 am 
Beginner
Beginner

Joined: Wed Nov 08, 2006 8:24 am
Posts: 26
Location: Gothenburg, Sweden
No, I don't save the newly child, I only add the object to the parent's list.

The list of parents is added to a binding source, will Hibernate get events when children are added or removed from the parent list?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2008 3:29 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
What kind of id generator do you use ? Can you post the mapping and the code for adding/removing ?

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.