Hibernate version: 3.1
Hi,
I'm currently working on a small project with struts and hibernate.
I use JavaBeans for representing the Entities in my database.
But I also use DTOs which are quite similar to the JavaBeans.
If I receive a list of Entities from the DB I use the following code:
Code:
public java.util.List getUserBOs() {
Session session = basis.util.HibernateUtil.currentSession();
org.hibernate.Query query = session.createQuery(
" select userBO " +
" from " +
" UserBO as userBO ");
return query.list();
}
In a so-called "ServiceClass" the properties of my EntityClass are saved into a DTO
Code:
public List<UserDTO> getUsers() {
UserBOFacade facade = new UserBOFacade();
UserBO userBO;
ArrayList users = new ArrayList();
Iterator<UserBO> iter = facade.getUserBOs().iterator();
while( iter.hasNext()){
userBO = iter.next();
users.add(new UserDTO(userBO.getUserId(),userBO.getUsername()));
}
// closeHibernateSession();
return user;
}
I sent this List to the view and display it.
But now here is my question:
If I perform a delete operation, I create a new UserBO with the right id when trying to execute session.delete(myUserBO); Hibernate throws an exception that an Object with the same reference (or key) is used in the same session, so the session is still open. But I thought that the HibernateSession is closed after a request.
If I close the HibernateSession in the getUser() Method (which is a comment at the moment) everything works fine.
But if the hibernate session is open while the application is running, lazy fetching isn't a problem after all in the view. Or did I misunderstood anything?
Thank you for your replys.