You are currently browsing legacy 3.0 version of documentation. Click here to switch to the newest 5.1 version.
Session: How to evict single entity from a session?
We can clear all session operations and stop tracking of all entities by using clear method, but sometimes there is need to only to do a cleanup only for one entity. For this purpose
evict was introduced.
public <T> void evict(T entity);
|entity||T||Instance of an entity that will be evicted|
Employee employee1 = new Employee(); employee1.setFirstName("John"); employee1.setLastName("Doe"); Employee employee2 = new Employee(); employee2.setFirstName("John"); employee2.setLastName("Shmoe"); session.store(employee1); session.store(employee2); session.advanced().evict(employee1); session.saveChanges(); // only 'Joe Shmoe' will be saved
Employee employee = session.load(Employee.class, "employees/1"); //loading from server employee = session.load(Employee.class, "employees/1"); // no server call session.advanced().evict(employee); employee = session.load(Employee.class, "employees/1"); //loading from server