I have created objects that have not yet been committed; within my code I need to query those objects. What happen is that the query hits the database and it always returns empty results. How can make my query hits the cache first then the database?
Asked
Active
Viewed 378 times
1 Answers
0
MyObject myObject = session.get(MyObject.class, identifier);
Taken from another post, emphasis mine.
The get() method is special because the identifier uniquely identifies a single instance of a class. Hence it’s common for applications to use the identifier as a convenient handle to a persistent object. Retrieval by identifier can use the cache when retrieving an object, avoiding a database hit if the object is already cached.
If you're looking to do a mass select/criteria instead of single entry select, then you need to commit the objects before doing the query. There is no way to mix and match objects from the cache and the database with a query.

Mason T.
- 1,567
- 1
- 14
- 33
-
I actually found a way to query the data before commiting them. I simply flushed the session (session.flush() ) and that resolved my problem. For info the flush process is when "the Session will execute the SQL statements needed to synchronize the JDBC connection's state with the state of objects held in memory" http://docs.jboss.org/hibernate/core/3.3/reference/en/html/objectstate.html#objectstate-flushing – oab Nov 17 '15 at 15:29