Question

I have three layers i.e Action,Service,DAO layers. I have loaded an object(of Employee class) from DB using hibernate with id 123.I have done some modifications to employee object.Later, I have create hibernate business object and done some modifications to that.

Employee e = service.getEmp(123);
e.setName("Ashok");

Order o = new Order
o.setNumber(567);

service.saveOrUpdate(o);

In this scenario, why it is trying to update employee object even though I not saying to save it? How to detach that from session?I don't want hibernate to update employee object.

Was it helpful?

Solution

In this scenario, why it is trying to update employee object even though I not saying to save it?

I quote from the hibernate docs:

Transactional persistent instances (i.e. objects loaded, saved, created or queried by the Session) can be manipulated by the application, and any changes to persistent state will be persisted when the Session is flushed. [...]. There is no need to call a particular method (like update(), which has a different purpose) to make your modifications persistent.

And

How to detach that from session?

Mark collections with cascade="evict". Then Session.evict(Object) the object before flushing on your object (if you have FlushMode.AUTO then maybe set it to MANUAL until you have done what you want).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top