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

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?
HappyEngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mr_ItCommented:
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 (3.2.4.1):
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).
0
HappyEngineerAuthor Commented:
Regardless of what the spec says, it doesn't appear that merge actually works that way.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.