-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate Session still open after Request? (Struts)
PostPosted: Wed Aug 09, 2006 7:52 pm 
Newbie

Joined: Thu Aug 03, 2006 5:50 am
Posts: 15
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.