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 6923383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:31:44+00:00 2026-05-27T10:31:44+00:00

I have a Hibernate project where a call to update() needs to compare the

  • 0

I have a Hibernate project where a call to update() needs to compare the modified object in memory to the data that has already been saved to the database. For example, my business logic states that if a record is “effective” (the effective date is today or earlier), an update cannot change the effective date. In order to accomplish this, I have the following code (it’s a little long and involved):

Manager

public class LogicManager {

  @Autowired
  SessionFactory sessionFactory

  private Session getSession() {
    return sessionFactory.getCurrentSession();
  }

  public MemberRecord findRecord(Integer id) {
    // << Code to check authorization >>
    return memberRecordDAO.findById(id);
  }

  public void updateRecord(MemberRecord record) {
    getSession().evict(record);
    MemberRecord oldRecord = memberRecordDAO.findById(record.getId());

    Date oldEffectiveDate = oldRecord.getEffectiveDate();
    if ( isEffective(oldEffectiveDate) && 
         !oldEffectiveDate.equals(record.getEffectiveDate)) {
      throw new IllegalArgumentException("Cannot change date");
    }

    // << Other data checks >>
    memberRecordDAO.update(record);
  }
}

DAO

public class MemberRecordDAO {
  @Autowired
  private SessionFactory sessionFactory;

  private Session getSession() {
    return sessionFactory.getCurrentSession();
  }

  public MemberRecord findById(Integer id) {
    return (MemberRecord)getSession()
             .getNamedQuery("findMemberById")
             .setInteger("id", id)
             .uniqueResult();
  }
}

Client Code

// ...
public void changeEffectiveDate(Integer recordId, Date newDate) {
  LogicManager manager = getBean("logicManager");

  MemberRecord record = manager.findById(recordId);
  record.setEffectiveDate(newDate);
  manager.updateRecord(record);
}

Before I added the evict() call in the Manager, I noticed that the manager was behaving in unexpected ways. In order to update a record, I’d first have to get that record by calling findById(), which would put the record into the Session cache. I’d make changes on that object, then call updateRecord() which would call findById() to get the (supposedly) persisted data. I realized that this second call to findById() would not look at the database data, but just pull the object from the cache. This would result in my oldEffectiveDate always being the same as my newly changed date, since record and oldRecord would be the exact same object.

To counteract this, I added the call to evict(), which I understood to mean that the object would be removed from the cache, forcing Hibernate to go to the database to get the MemberRecord. After I made that change, my MemberRecordDAO throws an exception when it calls uniqueResult(), which says AssertionFailed: possible nonthreadsafe access to session. When I run the debugger, I see that both LogicManager and MemberRecordDAO are using the same Session, which is what I thought was correct.

So, my questions:

  1. Is my thinking/algorithm correct? Is evict() the correct thing to do? Is there a better way? I am not too savvy on Sessions, caching or evict(). I want to make sure that this logic is correct before dealing with threading issues.
  2. Why is it that accessing the Session from the DAO is not threadsafe?
  • 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-27T10:31:45+00:00Added an answer on May 27, 2026 at 10:31 am

    This was the solution that passed my tests, but it still seems a little gross to me:

    Manager

      public void updateRecord(MemberRecord record) {
        MemberRecord oldRecord = record;
        record = record.clone();   //Added a clone() to MemberRecord
        getSession().evict(record);
        getSession().evict(oldRecord);
        getSession().refresh(oldRecord);
        // At this point, record has all of the new values, but none of the Hibernate
        //  data attached to it, due to the clone(). 
        // oldRecord is populated with the data currently in the database.
    
        Date oldEffectiveDate = oldRecord.getEffectiveDate();
        if ( isEffective(oldEffectiveDate) && 
             !oldEffectiveDate.equals(record.getEffectiveDate)) {
          throw new IllegalArgumentException("Cannot change date");
        }
    
    
        // << Other data checks >>
        memberRecordDAO.update(record);
      }
    

    If this type of thing can be done cleaner, please tell me.

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

Sidebar

Related Questions

We have a project that uses JPA/Hibernate on the server side, the mapped entity
I am using hibernate, struts, extjs in my project. I have a Customer object
I'm working on a java project full of Hibernate (3.3.1) mapping files that have
i have 3 maven project (starto.commons,starto.hibernate,starto.server) that use some same dependencies and two of
I have been working with a project which is using grails / hibernate with
I have a bit old project that I would call legacy. Some characteristics of
I have a maven project A that use hibernate-annotations 3.4.0.GA which use the slf4j-api
Is there any documentation about introducing jBoss seam to an old Hibernate/JSF project? Have
I have a project with spring and hibernate in GWT, I am using below
We have an object, A, which contains another object, B. We have Hibernate calling

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.