Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Employee emp = (Employee) session.get(Employee.class, empId); emp.setSalary(newSalary); session.disconnect(); emp.setSalary(anotherSalary); // This change will not be tracked by the session tx.commit(); session.close();
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Query query = session.createQuery("FROM Employee"); query.setFetchSize(BATCH_SIZE); ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY); int count = 0; while (results.next()) { Employee emp = (Employee) results.get(0); session.detach(emp); // Detach each employee object after loading if (++count % BATCH_SIZE == 0) { session.flush(); session.clear(); // Clear the session cache to avoid OutOfMemory error } } tx.commit(); session.close();The `disconnect()` method is part of the `org.hibernate.Session` class, which is part of the Hibernate core library.