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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

632 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