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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:49:05+00:00 2026-05-26T04:49:05+00:00

firstly please excuse my relative inexperience with Hibernate I’ve only really been using it

  • 0

firstly please excuse my relative inexperience with Hibernate I’ve only really been using it in fairly standard cases, and certainly never in a scenario where I had to manage the primary key’s (@Id) myself, which is where I believe my problems lies.

Outline: I’m bulk-loading facebook profile information through FB’s batch API’s and need to mirror this information in a local database, all of which is fine, but runs into trouble when I attempt to do it in parallel.

Imagine a message queue processing batches of friend data in parallel and lots of the same shared Likes and References (between the friends), and that’s where my problem lies.

I run into repeated Hibernate ConstraintViolationException’s which are due to duplicate PK entries – as one transaction has tried to flush it’s session after determining an entity as transient when in fact another transaction has already made the same determination and beaten the first to committing, resulting in the below:

Duplicate entry '121528734903' for key 'PRIMARY' 

And the ConstraintViolationException being raised.

I’ve managed to just about overcome this by removing all cascading from the parent entity, and performing atomic writes, one record per-transaction and trying to essentially just catching any exceptions, ignoring them if they do occur as I’d know that another transaction had already done the job, but I’m not very happy with this solution and cant imagine it’s the most efficient use of hibernate.

I’d welcome any suggestions as to how I could improve the architecture…

Currently using : Hibernate 3.5.6 / Spring 3.1 / MySQL 5.1.30

Addendum: at the moment I’m using a hibernate merge() which checks initially for the existence of a row and will either merge (update) or insert dependant on existence, problem is even with an isolation level of READ_UNCOMMITTED sometimes the wrong determination is made, i.e. two transactions decide the same, and I’ve got an exception again.

Locking doesn’t really help me either, optimistic or pessimistic as the condition is only a problem in the initial insert case and there’s no row to lock, making it very difficult to handle concurrency…

I must be missing something but I’ve done the reading, my worry is that not being able to leave hibernate to manage PK’s i’m kinda scuppered – as it checks for existence to early in the session and come time to synchronise the session state is invalid.

Anyone with any suggestion for me..? thanks.

  • 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-26T04:49:05+00:00Added an answer on May 26, 2026 at 4:49 am

    Take this with a large grain of salt as I know very little about Hibernate, but it sounds like what you need to do is specify that the default mysql INSERT statement is instead made an INSERT IGNORE statement. You might want to take a look at @SQLInsert in Hibernate, I believe that’s where you would need to specify the exact insert statement that should be used. I’m sorry I can’t help with the syntax, but I think you can probably find what you need by looking at the Hibernate documentation for @SQLInsert and if necessary the MySQL documentation for INSERT IGNORE.

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

Sidebar

Related Questions

Please understand firstly that I fully understand that Java will return a String when
Firstly, Real World Haskell , which I am reading, says to never use foldl
Firstly, please go to this url: celebritywallpaperbase picture #1 is Drew Barrymore smile picture
Firstly this has turned out to be quite a long post so please bear
Firstly, this DB question could be a bit DB agnostic, but I am using
Firstly, I know I should be using proper Threading techniques (Threadpool, BeginInvoke, etc.) to
Firstly, please don't move to serverfault. It is indeed a programming question :-) We
Firstly, please don't dismiss this question - I'm aware it's an ugly situation but
Could you please explain me the logic of UNIX signal system: firstly it sends
Firstly, I'm totally javascript inept so please explain throughly. OK, I have two issues

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.