What is the proper way to use JPA to attempt persist and then merge if it exists? (INSERT or UPDATE)

Posted on 2008-11-27
Last Modified: 2012-05-05
There is a pattern when attempting to store things in the DB. In some situations there may be multiple threads attempting to create the same object, so the best way to deal with this is to attempt to insert the object and then, if an exception is thrown, update the object instead.

But, I don't know how to do that properly in JPA. If I try to persist an object inside of a transaction, the failure exception won't be thrown until the transaction attempts a commit. So I have to do the persist inside of a transaction by itself. But, if I do that, hibernate logs a huge long stack trace for org.hibernate.exception.ConstraintViolationException even though I am already catching and handling it.

Ultimately what I do does work. But every single failed insert results in a huge stack trace. I don't want to use log4j to block those stack traces because I sometimes those messages could be for actual errors.

What should I do to properly implement the INSERT or UPDATE pattern using JPA?
Question by:HappyEngineer

Expert Comment

ID: 23064920
I believe you can also call merge on new entity instances. A new record will be created if necessary on flush or transaction commit.

In the specs you can read (
The semantics of the merge operation applied to an entity X are as follows:
If X is a new entity instance, a new managed entity instance X' is created and the state of X is
copied into the new managed entity instance X'.

So you might wonder why they also added the persist method? I can think of use cases where you want to be sure that a new record is created (instead of updated). Or a certain user migth only be permitted to create new records, not to update them. Also note that the persist operation is the most performant of both (the instance is not copied).

Accepted Solution

HappyEngineer earned 0 total points
ID: 24651177
Regardless of what the spec says, it doesn't appear that merge actually works that way.

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Facing this issue for maven proxy setting 2 25
mysql jsp example issue 32 69
Problem to Alipay 10 67
Code Coverage and Coding Standards 2 43
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question