Hibernate First Level cache Example

Hello friends, today I would like to discuss Hibernate First Level Cache which is available to any valid hibernate session . So it means all the objects which are in the persistent state are available in first level cache until and unless the session is clear or objects are evicted from the session.Hibernate by default provide the First level cache .
Here is the sample code which can demonstrate the feature.
Using the previously  posted example… http://onetouchcode.com/2012/12/05/simple-hibernate-example/

Hibernate First Level CacheSample Code


public class FirstLevelCacheIlluastrationClass {
public static void main(String[] arg){
 Configuration configuration = new Configuration();
     SessionFactory    sessionFactory =configuration.buildSessionFactory();
    Session session=sessionFactory.openSession();
    Transaction tx=null;
    StudentClass stuObject=new StudentClass();
    stuObject.setStudentName("Nitesh Sahay"); 
    String studentId=session.save(stuObject);

    stuObject=session.load(StudentClass.class,studentId);;//Able to get the student //from First level cache
}catch(Exception e){
   e.printStackTrace();//session already closed... no proxy



Hope this will help you to understand ,what is hibernate 1st level cache.

We will come up with more hibernate related post, till then stay tuned and Happy Coding!


Leave a Reply

Your email address will not be published. Required fields are marked *