Reputation: 31
I want to detach object from session so I written code like is shown below. But it is not detaching from session, instead it is saving to the database.
How evict()
and clear()
methods work, can anybody answer please?
session.evict(student);
session.save(student);
Upvotes: 2
Views: 1704
Reputation: 1161
I think you should call session.save(student);
and then session.evict(student);
or session.clear();
as it will not make sense vice versa.
Answering your original question session.evict(student);
will remove the object from the session while session.clear();
will completely clear the session.
clear() Completely clear the session.
evict(Object object) Remove this instance from the session cache.
You could refer the official documentation at https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/Session.html. Hope that helps
Upvotes: 2