File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes hibernate cache 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 cache" Watch "hibernate cache" New topic
Author

hibernate cache

sanjey asok
Ranch Hand

Joined: Jul 11, 2009
Posts: 39
In hibernate caching, If I execute a query ,that result set will be stored in Cache,
Later ,it is used if the same query is executed

1)If I have a table with 50 Records and I used select statement to select all records in the table, after the query executed, if result set is cached by Hibernate, then I add some data into the table, again I execute the same query, how the hibernate will handle this scenario, whether it will execute query fresh and cache the result ? or some other best way???
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Hibernate only uses a query cache if you configure it to do so, and if you do you also need to provide a second level cache. The query cache does not store the state of your results it stores a result set of identifiers, the state is held in the second level cache. Using the query cache and the second level cache are not things you would normally do, the query cache in particular would be one of the last mechanisms you would probably look to to improve performance.


1)If I have a table with 50 Records and I used select statement to select all records in the table, after the query executed, if result set is cached by Hibernate, then I add some data into the table, again I execute the same query, how the hibernate will handle this scenario, whether it will execute query fresh and cache the result ? or some other best way???

Hibernate will go to the cache until it is flushed. If you udate data outside of Hibernate there is no way it can know about this update, such are the dangers of using any caching mechanism with a database. you need to choose carefully when to use one.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
sanjey asok
Ranch Hand

Joined: Jul 11, 2009
Posts: 39
If you udate data outside of Hibernate there is no way it can know about this update



if it is updated using Hibernate ,please explain such scenario
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Because Hibernate is managing the sate it can manage the cache, so there is no issue if Hibernate is the only mechanism that updates your data. In the real world however this is not normal behaviour, databases are shared so multiple client can update the data in multiple ways. Hence thinking long and hard about what aspects of caching you might use and whether it is appropriate is key.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: hibernate cache