-->
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.  [ 7 posts ] 
Author Message
 Post subject: Object Set Does Not Reflect actual Database State
PostPosted: Tue Nov 15, 2005 5:08 pm 
Beginner
Beginner

Joined: Tue Sep 20, 2005 11:01 am
Posts: 25
We are using Hibernate 2.1

Our application receives xml files via web services that it then stores to our backend database using Hibernate. Each xml file translates into 10 - 20 backend database records that must be saved. These database records are all saved using the same Hibernate Session.

An example of the problem we are running into is as follows:

The xml file comes in, and using jaxb, is mapped to pojos. We use these pojos to initialize the hibernate objects that get saved to the database. During this process we save several hibernate objects. One is a "parent" object (parentObj) and several of the others are "child" objects (childObjs) of that parent.

I save the parentObj first and then save each childObj in turn. I assumed the parentOjb's Set of childObjs would reflect what was already saved to the database. What I find is that the parentObj's Set of childObjs is null. In other words the the parentOjb's Set of childObjs do not actually reflect what is in the database. To rectify this problem I had to do a Session.refresh(parentObj). This forced the parentOjbs data to be re-read from the database. After doing this the parentObj's Set of childObjs accurately reflected what was in the database.

Can someone explain why this is happening?

We are using level 1 cache only.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 15, 2005 9:47 pm 
Beginner
Beginner

Joined: Tue Oct 18, 2005 3:57 pm
Posts: 48
Location: Los Angeles, CA
You can force Hibernate to sync with database by close()ing or flush()ing the session. If you don't, session (aka. 1st-level cache) will keep objects around in memory until something forces it to sync up with the db.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 16, 2005 9:36 am 
Beginner
Beginner

Joined: Tue Sep 20, 2005 11:01 am
Posts: 25
I should have been more explicit. We are flushing after each save that we do.

Are you saying that Hibernate is not able to sync updates to the database even when the updates are all done using level 1 cache using the same Session?

Do I need to do a Session.clear() and force the database to be re-read the data to ensure the object accurately reflects what's in the database?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 16, 2005 11:10 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Are you saving the parent, then in a separate set of save commands saving the children by themselves?

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 16, 2005 1:31 pm 
Beginner
Beginner

Joined: Tue Sep 20, 2005 11:01 am
Posts: 25
Yes, although it is all done in the same Hibernate Session. Should I be doing something differently?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 16, 2005 3:40 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
yes.

you should be only doing one save. Build up the parent object with its children inside it before you save.

Code:
Parent p = new Parent();
p.setName
....
Child c1 = new Child();
..
p.addChild(c1);
...

p.save();


if you have the cascade options set correctly this one save will save the parent and all the children into the database and the parent will exist in memory with all its children set not requiring an extra update.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 9:51 am 
Beginner
Beginner

Joined: Tue Sep 20, 2005 11:01 am
Posts: 25
Many thanks, that's what I was looking for.


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