Hibernate entities stored as HttpSession attribute values

Posted by njudge on Stack Overflow See other posts from Stack Overflow or by njudge
Published on 2010-05-29T18:28:19Z Indexed on 2010/05/29 18:32 UTC
Read the original article Hit count: 167

Filed under:
|
|
|

I'm dealing with a legacy Java application with a large, fairly messy codebase. There's a fairly standard 'User' object that gets stored in the HttpSession between requests, so the servlets do stuff like this at the top:

HttpSession session = request.getSession(true);
User user = (User)session.getAttribute("User");

The old user authentication layer (which I won't describe; suffice to say, it did not use a database) is being replaced with code mapped to the DB with Hibernate. So 'User' is now a Hibernate entity.

My understanding of Hibernate object life cycles is a little fuzzy, but it seems like storing 'User' in the HttpSession now becomes a problem, because it will be retrieved in a different transaction during the next request. What is the right thing to be doing here? Can I just use the Hibernate Session object's update() method to reattach the User instance the next time around? Do I need to?

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate