50 likes | 70 Views
Caching is facility provided by ORM framework which help users to get fast running web application, while help framework itself to reduce number of quaries made to database in a single transaction. Hibernate achives the second goal by implementing first level cache.<br>First level cache is enabled by default and you can notdiasable it.<br>First level cache is associated with session object and other session objects in application can not see it.<br>The scope of session cache objects is of session. Once session is closed, cached object is gone forever.
E N D
Welcome to Ducat India Apply Now Language | Industrial Training | Digital Marketing | Web Technology | Testing+ | Database | Networking | Mobile Application | ERP | Graphic | Big Data | Cloud Computing Call us: 70-70-90-50-90 www.ducatindia.com
Hibernate First Level Cache with Example • Understanding Hibernate First Level Cache With Example. • Caching is facility provided by ORM framework which help users to get fast running web application, while help framework itself to reduce number of quaries made to database in a single transaction. Hibernate achives the second goal by implementing first level cache. • First level cache is enabled by default and you can notdiasable it. • First level cache is associated with session object and other session objects in application can not see it. • The scope of session cache objects is of session. Once session is closed, cached object is gone forever. • First level cache retrieval example • In this example, we are fetching student object from the database using a hibernate session. we will retrieve it multiple times and will observe the SQL logs to see the differences.
packagenet.ducatindia.hibernate.tutorial; importorg.hibernate.Session; importorg.hibernate.Transaction; importnet.ducatindia.hibernate.tutorial.config.HibernateJavaConfig; importnet.ducatindia.hibernate.tutorial.entity.Student; publicclassFirstLevelCacheDemo { publicstaticvoidmain(String[] args) { Transactiontransaction=null; try (Sessionsession=HibernateJavaConfig.getSessionfactory().openSession()) { // start the transaction transaction=session.beginTransaction(); // get the student entity using id Student student1 =session.load(Student.class, newLong(1));
System.out.println(student1.getFirstName()); System.out.println(student1.getLastName()); System.out.println(student1.getEmail()); // load student entity by id Student student2 =session.load(Student.class, newLong(1)); System.out.println(student2.getFirstName()); System.out.println(student2.getLastName()); System.out.println(student2.getEmail()); // commit transaction transaction.commit(); } catch (Exception e) { if (transaction !=null) { transaction.rollback(); } } } } Output: Read More: https://tutorials.ducatindia.com/java/hibernate-first-level-cache-with-example/
Thank You Call us: 70-70-90-50-90 www.ducatindia.com