Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 936457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:19:05+00:00 2026-05-15T21:19:05+00:00

I’m dealing with a legacy Java application with a large, fairly messy codebase. There’s

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:19:06+00:00Added an answer on May 15, 2026 at 9:19 pm

    Assuming that you are creating a new hibernate session for each request-response cycle, it is possible to merge a detached object into the new hibernate session, but I would avoid this approach altogether.

    Instead try storing a key on the HttpSession that can be used to look up a User through hibernate for every incoming request. If you are worried about the performance consequences of visiting the database to retrieve something that can be stored in the HttpSession instead, fear not – you can always use a caching framework supported by hibernate to reduce the number of database visits. Another way to improve performance would be to use optimistic locking.

    Although I have not looked at the hibernate sources, I think hibernate uses the “Identity Map” pattern. This is a Map that uses the entity’s id as a key in the Map and the associated entity object as a value in the Map. Whenever an entity is retrieved from the hibernate session, hibernate will look at session’s identity map to see if it is there. If it is there it will return the entity from the map. If it is not there it will retrieve the entity from the database and put it on the map, and then return the entity. This means that consecutive queries that access a given User with the same key (ie id, userId etc) for a given hibernate session will receive a reference to the same User object, and therefore each query will be able to “see” modifications made to the User object by the other query. For this reason it is absolutely imperative to create a new hibernate session for each incoming request, so that concurrent requests for a given User do not have to lock their respective threads on their common User object. Different hibernate sessions will each have their own identity map and therefore will not return references to the same User object.

    By attempting to merge a User object from the HttpSession into your hibernate session you are basically trying to manipulate hibernate’s IdentityMap directly, replacing whatever hibernate “thinks” ought to be there with something else, and understandably this can cause problems. As I said although it is possible to attach a detached object back into a hibernate session I would avoid it. Good luck with whatever approach you take.

    I would highly recommend reading the following, in your case particularly the sections on long conversations and detached objects:

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 480k
  • Answers 480k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Check this solution out. It has worked for me in… May 16, 2026 at 6:15 am
  • Editorial Team
    Editorial Team added an answer If you just need a lightweight application, you can have… May 16, 2026 at 6:15 am
  • Editorial Team
    Editorial Team added an answer Basically, you're passing in a list of values to use… May 16, 2026 at 6:15 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.