2

I get an entity 'A' using

getHibernateTemplate().get(A.class, 100) 

from the database. Lets say this entity 'A' has a property 'value' 200 in the database.

Now, in my Java code, I change a property for this entity. lets say, I change the 'value' property to '500' and then add it to some list.

Now, If I again do getHibernateTemplate().get(A.class, 100) for the same Entity, I am getting the updated entity(that has a value of 500). How do I force hibernate to get me the entity from the database, but not the one updated in my code?

Is this what is called as 'First Level Caching'?

user1717230
  • 443
  • 1
  • 15
  • 30

2 Answers2

3

Your assumption (about first level caching) is correct. As for example stated here: Interface Session:

The main runtime interface between a Java application and Hibernate. This is the central API class abstracting the notion of a persistence service.

Or here Chapter 2. Architecture; 2.1. Overview

Extract: Session (org.hibernate.Session)

A single-threaded, short-lived object representing a conversation between the application and the persistent store. It wraps a JDBC connection and is a factory for Transaction. Session holds a mandatory first-level cache of persistent objects that are used when navigating the object graph or looking up objects by identifier.

And also, you can see the methods available for us, to remove an object form the session:

  • evict(Object object): Remove this instance from the session cache.

  • refresh(Object object): Re-read the state of the given instance from the underlying database.

  • clear(): Completely clear the session.

And many more. Evict in this case should be working. We have to take the current instance ('A') and explicitly Evict it from the session.

If we've already loaded some/more stuff, and we do not know, what to Evict(), we simply need to get the fresh data. Then we can call Clear() to completely reset the session and start again.

This is a bit radical, because none of the objects in the session will be updated/inserted on session Flush()... but it could be what we want in this scenario (very often used for testing... load, clear... change and flush)

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
0

I suggest searching Google for hibernate commit, flush, and detach and reading up on when they write to the database. Better yet, I recommend reading a good book on Hibernate if you haven't already done so (search amazon.com for good reviews on a book) to get a good grasp of the technology.

My reason for responding to this post is not to answer your question directly, but suggest that you edit your hibernate.cfg.xml file and set the following to true: < property name="hibernate.show_sql" > false < /property >. This will cause a display to your console window to list when every sql statement that is sent to the database. This way, you can see exactly when a write to the database occurs. You can then experiment with what you research/read and verify it works as you expect.

user2810910
  • 279
  • 1
  • 2