I use Hibernate and I'm having trouble with persistent objects and transactions.
In my business layer I call my DAO methods (e.g. load) that in turn call HibernateTemplate.load and return the persisted object to the business layer.
There I want use it as it were a Transfer Object, changing some of its values, but as it's done inside a transaction the object is cached in the session and every change I made to it is persisted when closing the session at the end of the transaction (are these assumptions true?)
Seems that there's no method to dettach persisted objects from the session and maybe a solution could be return a clone of the persisted object in the DAO or creating a Transfer Object with data from the persisted object.
If anyone asks about it I don't want to use the OpenSessionInView filter as it breaks the layered architecture.
And a sample can be a use case where I want to return a department with only the employees that have some attribute. I load the department object with a lazy collection of employees calling dao.load(). Then I can call dao.getEmployeesXXX that uses a filter to get only a subset of employees from the lazy collection).
To sum up:
if department is the same object as returned by Hibernate then I'll get errors or it will be update in database with only those employees from dao.getEmployeesXXXCode:department = dao.load(id); department.setEmployees(dao.getEmployeesXXX(department));
Thanks in advance.