Hibernation objects stored as HttpSession attribute values

I am dealing with an outdated Java application with a large, rather dirty code base. There is a pretty standard User object that is stored in HttpSession between requests, so servlets do things like this at the top:

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

The old user authentication level (which I will not describe, suffice it to say that he did not use the database) is replaced by the code associated with the database with Hibernate. Thus, the "User" is now a Hibernate object.

My understanding of the life cycles of the Hibernate object is a bit fuzzy, but it seems that saving “User” in HttpSession is now becoming a problem because it will be retrieved in another transaction during the next request. What is the right thing here? Can I just use the update method of the Hibernate session object to reconnect the user instance next time? I need?

+3
source share
3 answers

Assuming you create a new hibernate session for each request-response cycle, you can combine a separate object into a new hibernation session, but I would completely avoid this approach.

HttpSession, . -, HttpSession, - , , . .

, , hibernate "Identity Map". , . , , , , . , . , , . , , (.. id, userId ..) , , "" , User . , . .

User HttpSession hibernate, IdentityMap, , "" - , , . , , . .

, , , :

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/transactions.html

+1

session.merge(..)

(From Session docs):

, merge().

, hashCode() equals().

0

, , .. . () db, .

, , . .

0

Source: https://habr.com/ru/post/1747686/


All Articles