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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:54:11+00:00 2026-05-19T16:54:11+00:00

Consider the scenario: A Db transaction envolving more than one row from different tables

  • 0

Consider the scenario:
A Db transaction envolving more than one row from different tables with versioning.

For example:
A shopLists and products. Where a shopList may contain products (with their amount in the shoplist) and products have their current stock.

When I insert ou edit a shopList, I want the stock of those products in the shopList to be updated to keep the stock consistant.

To do that, I open a transaction, insert/update the shopList, update the stocks for each product (apply delta) and then commit the transaction. No big deal up to now.

However, other user may have updated one or more products in common. Or even updated the shopList itself. In both cases, I would get a StaleObjectStateException when commiting the transaction.

Question is: Is there a way to determine which table caused the StaleObjectStateException?

In case the product caused the exception, I could refresh all envolved products from the DB and then reapply the stock deltas. And that’s fine.
In case the shopList caused the exception, it would be better to simply report the issue to the user so he could start over again.

Thanks very much for your help.

  • 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-19T16:54:12+00:00Added an answer on May 19, 2026 at 4:54 pm

    I found out how.

    First things first:
    the JPA (or the hibernate itself) wraps the org.hibernate.StaleObjectStateException exception as javax.persistence.OptimisticLockException.
    So, if you want to catch the right exception, go for OptimisticLockException.

    Second:
    The hibernate will only throw the OptimisticLockException when you call EntityManager’s method Flush before commit. If you call Commit directly you will get another exception instead (I’ve forgot which). Considering almost everyone catches those exceptions issued by the commit method and go for a transaction rollback, you’ll get a Rollback’s related exception (can’t remember which once again).

    Third and finaly answering my original question:
    You just have to call the getEntity method from the OptimisticLockException instance to get the origin of the Versioning error. That’s going to provide you anything you need related to that.

    Thanks for all of those who passed by here.
    Any questions regarding that, just ask and I’ll be glad to help.

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

Sidebar

Related Questions

Consider a scenario where a Mirth channel has one or more Destinations. In this
From couple of days i am thinking of a following scenario Consider I have
Consider this scenario: I have a project with two modules and one common module
Consider the scenario below: The application is multi-threaded and it has one static ArrayList
Consider a scenario where a console application asks for a series of inputs, one
Consider this scenario that two WCF clients connect to one WCF service(server), this service
Consider a scenario where you'd like to pull the last x entries from a
Consider a scenario that a java program imports the classes from jar files. If
I remember I've read this from somewhere but still unsure. consider scenario below: <action
This is a contrived example, but consider a scenario where employees have working locations,

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.