Q1:What is the of use caching in hibernate? Can we disable the first level cache?
Caching helps in reducing the hits over database.First level cache cannot be disabled but by using evict() we can make hibernate to fetch the entity everytime from db
- First level cache is associated with “session” object and other session objects in application can not see it.
- The scope of cache objects is of session. Once session is closed, cached objects are gone forever.
- First level cache is enabled by default and you can not disable it.
- When we query an entity first time, it is retrieved from database and stored in first level cache associated with hibernate session.
- If we query same object again with same session object, it will be loaded from cache and no sql query will be executed.
- The loaded entity can be removed from session using evict() method. The next loading of this entity will again make a database call if it has been removed using evict() method.
- The whole session cache can be removed using clear() method. It will remove all the entities stored in cache.
You should therefor either use the evict() or clear() method to force a query to the database.
Please Refer here
Q2:What is automatic dirty checking in hibernate?
Whenever an entity is loaded through hibernate, it makes an additional copy of that whole entity object along with the all entity’s property values. By entity, we mean each persistent object of that entity type. Within the Persistence Context, Hibernate has a copy of all persistent objects that were loaded from the database. It compares these persistent objects with these objects to detect the objects that have been modified or are dirty. This is the default implementation.
Please Refer here