Quick Contact

    Hibernate

    Hibernate Session.clear() Method Example

    The Session.clear() method is used to remove all cached objects associated with the session.

    voidorg.hibernate.Session.clear()

    This method is used to completely clear the session. Evict all loaded instances and cancel all pending saves, updates, and deletions. Do not close open iterators or instances of ScrollableResults.

    Hibernate Session.clear() Method Example

    The Session.clear() method is used to remove all cached objects associated with the session.

    packagenet.ducatindia.hibernate.tutorial;
    
    importorg.hibernate.Session;
    importorg.hibernate.Transaction;
    
    importnet.ducatindia.hibernate.tutorial.config.HibernateJavaConfig;
    importnet.ducatindia.hibernate.tutorial.entity.Student;
    
    public class RemoveFirstCacheDemo {
    
    public static void main(String[] args) {
    
            Transaction transaction = null;
    try (Session session = HibernateJavaConfig.getSessionfactory().openSession()) {
    
                // start the transaction
    transaction = session.beginTransaction();
    
                // get the student entity using id
                Student student1 = session.load(Student.class, new Long(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, new Long(1));
    System.out.println(student2.getFirstName());
    System.out.println(student2.getLastName());
    System.out.println(student2.getEmail());
    
    session.clear();
                // load student entity by id
                Student student3 = session.load(Student.class, new Long(1));
    System.out.println(student3.getFirstName());
    System.out.println(student3.getLastName());
    System.out.println(student3.getEmail());
    
                // commit transaction
    transaction.commit();
    
            } catch (Exception e) {
    if (transaction != null) {
    transaction.rollback();
                }
            }
        }
    }
    

    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.