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

  • Home
  • SEARCH
  • 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 830415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:02:23+00:00 2026-05-15T04:02:23+00:00

I just noticed that Hibernate entities are automatically persisted to the database (or at

  • 0

I just noticed that Hibernate entities are automatically persisted to the database (or at least to cache) before I call any save() or update() methods. To me this is a pretty strange default behavior — but ok, as long as I can disable it, it’s fine.

The problem I have is that I want to update my entity’s state (from a state “1” to state “2”) only if the entity in the database still has the state it had when I retrieved it (state “1”). This is to eliminate concurrency issues when another server is updating this same object. For this reason, I have created a custom NamedQuery that will only update the entity if it is in the expected state “1”. Here is some pseudo-code:

// Get the entity
Entity item = dao.getEntity(); 
item.getState(); // == 1

// Update the entity
item.setState(2); // Here is the problem, this effectively changes the
                  // state of my entity breaking my query that verifies
                  // that state is still == 1.

dao.customUpdate(item); //Returns 0 rows changes since state != 1.

How do I make sure the setters don’t change the state in cache/db?

  • 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-15T04:02:24+00:00Added an answer on May 15, 2026 at 4:02 am

    Hibernate has built in support for what you are aiming to achieve – it supports optimistic locking using version ids or timestamps. If you try to save an object and the underlying data has changed, hibernate throws an exception. You can catch this exception and handle as necessary – ignore the update, retry or whatever makes sense.

    See Optimistic Concurrency Control

    EDIT: I’m not clear on how your update query works, but if you want to continue doing this by hand, you can evict your object from the session after retreiving it. This gives you a snapshot of the object as it was at retrieval time that you can modify independent from the db/cache. You can pass it into your custom update query as one side of the comparison, the other side being the persisted value. When the query selects/updates the entity, it will be re-read from the database, not from your evicted object. Your update query can then compare this with your evicted/modified value (passed as a parameter) to see if the update should be done or not.

    See Session.evict()

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

Sidebar

Related Questions

Just noticed that the style of the navigation menu on windows.com is just what
I just noticed that the return list for results is limited to 1000. I
I just noticed that Chromium was installed in AppData in both Vista and XP.
I just noticed that you can do this in C#: Unit myUnit = 5;
I just noticed that you can not use standard math operators on an enum
I have just noticed that a multidimensional array in C# does not implement IEnumerable<T>
I've just noticed that tidy_repair_string() is removing my non-breaking spaces from empty elements causing
I'm trying to use LinqToEntities and just noticed that there are no foreign key
I'm using the MIDP 2.0 (JSR 118) and I just noticed that there is
I've noticed that just in the last year or so, many major websites have

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.