This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Object Relational Mapping and the fly likes Hibernate should hit db every single time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate should hit db every single time" Watch "Hibernate should hit db every single time" New topic
Author

Hibernate should hit db every single time

Simon Ship
Greenhorn

Joined: Apr 20, 2005
Posts: 2
Hi,

My application has web services component which updates the database. I am using Hibernate 2.1 to retrieve the data from the database.

The sequence of event is like this.

1) Retrieve a record from a table using Hibernate.
2) Using web service the record is updated.
3) The application performs the query again. The stale value is returned by the hibernate.

I'm sure there must be some way to tell Hibernate that it doesn't have full control over the database and should go to the DB for every single query.

How do I solve this problem.

Thanks.
/Simon
Sujatha Kumar
Ranch Hand

Joined: Jan 05, 2004
Posts: 134
Hi Simon,

How about trying with lazy="true".
It will load the records lazily upon request.


SCJP,SCWCD,SCBCD,SCEA Part I
Simon Ship
Greenhorn

Joined: Apr 20, 2005
Posts: 2
Using lazy feature will only make sure that the associated classes will be loaded only when it's needed.

My question still remains same. Let's say that a record is lazily loded. Web service updates the record. You query for the record, the stale record will show up because Hibernate is not aware that some other system has changed the record behind-the-scene.

/simon
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Normally you'd open a new Session for each application transaction. Since each Session starts empty, Hibernate would hit the database again. Of course, if you're using a second level cache, you'd still need to address that. If you want to reuse the Session, you could evict() the object before loading it. I'm not sure if this affects the second level cache, though.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate should hit db every single time