It looks simple question .I tried a lot understand but failed.
As per my knowledge Hibernate first level cache means session level cache.When we retrieve the same object more than once in same session it will retrieve from Cache.
For example I have an Employee record with id 100 in database.
I opened a session and get that employee object.Until I close session that object is available in the same session.
Question:Why I need to retrieve same object more than once in same session(Any how it is available in session until I close it)?
As per my knowledge Hibernate first level cache means session level cache.When we retrieve the same object more than once in same session it will retrieve from Cache.
It is half-correct. Apart from what you said, one major reason for first level cache is, under same session, Hibernate will ensure same entity (entity with same ID) is going to be represented by same object instance.
What you said is correct only if you are getting the entity from session by ID:
Foo foo1 = session.get(Foo.class, 1L);
Foo foo2 = session.get(Foo.class, 1L);
First call of get()
will go to DB to load Foo
. When 2nd call is invoked, Hibernate will check if within this session, any Foo
with ID 1 has already been retrieved. As it has been retrieved before, Hibernate will simply get that Foo
instance and return to you.
However, this scenario is not the most common situation that you will see first level cache in effect. Consider this:
// psuedo code only
User user = findByUserName("ADRIAN"); // assume ID = 777
List<User> users = findAllActiveUsers();
(Assuming the above finders are internally running query thru Hibernate session) When Hibernate run the 2nd query, internally Hibernate is running the SQL, get the resultset, and convert each record to a User. Assuming one of the active users is having ID 777. When Hibernate construct that User object instance, it will first check if it exists in first level cache. Because it was previously retrieved (in previous query which find by user name), instead of constructing a new User object instance, Hibernate will simply reuse the instance that previously constructed (and stored in 1st level cache) and use it in the result list.
By doing so, Hibernate can ensure, within the same session, if you retrieve same entity (same class with same ID) by different ways, you can always assume that entity will be same object instance.
Think of a more complicated example, that you are trying to retrieve Order
s from your system, which refers to User
(assume Many-To-One). What you will discover is, different Order
, whenever it refers to same User
(in DB), it is actually referring to same User
object instance.
For the question of
how it is available in session until I close it
it is more an internal implementation detail of Hibernate. However, conceptually, you can imagine it as, each Session is internally having a Map, the key is Entity Type + ID, the value is the entity object instance.
When you query from DB and when session construct the entities for you, for each entity, it will lookup from the map if it is already there. If not, session will construct the entity and put in the map. If it is already there, session will simply make use of the entity in the map
Similar idea when you are getting entity by ID (thru Session.get()
or Session.load()
etc)