I have a non-bean managed session with an injected EntityManager em.
What I'm trying to do is have a database table with a unique column. Then I run some algorithm that tries to insert an object. If an entity exists, it will update it or skip it.
I would like to have something like this:
try { em.persist(cd); em.flush(); } catch (PersistenceException e) { // Check if the exception is DatabaseException and ConstraintViolation // Update instead or skip it }
The problem is that I can only catch a PersistenceException . DatabaseException not caught. This is sad because only DatabaseException has a method called getDatabaseErrorCode() , which I would like to use to check for duplicate records. I do not understand this because PersistenceException.getCause() returns a DatabaseException .
So my question is: how to catch DatabaseException and check MySQL error code?
Thank you for any ideas and experience.
source share