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?