Skip to content

Commit

Permalink
throw EntityExistsException as required by JPA spec 3.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed Sep 4, 2023
1 parent 4af9e50 commit 42d8680
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.hibernate.JDBCException;
import org.hibernate.LockOptions;
import org.hibernate.ObjectNotFoundException;
import org.hibernate.PersistentObjectException;
import org.hibernate.StaleObjectStateException;
import org.hibernate.StaleStateException;
import org.hibernate.TransientObjectException;
Expand Down Expand Up @@ -120,7 +121,8 @@ else if ( exception instanceof ObjectNotFoundException ) {
rollbackIfNecessary( converted );
return converted;
}
else if ( exception instanceof org.hibernate.NonUniqueObjectException ) {
else if ( exception instanceof org.hibernate.NonUniqueObjectException
|| exception instanceof PersistentObjectException) {
final EntityExistsException converted = new EntityExistsException( exception.getMessage(), exception );
rollbackIfNecessary( converted );
return converted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

import java.util.ArrayList;
import java.util.Collection;

import jakarta.persistence.EntityExistsException;
import jakarta.persistence.PersistenceException;

import org.hibernate.PersistentObjectException;
import org.hibernate.dialect.AbstractHANADialect;
import org.hibernate.exception.ConstraintViolationException;

Expand Down Expand Up @@ -187,9 +188,9 @@ public void testCreateExceptionWithGeneratedId(SessionFactoryScope scope) {
session.persist( dupe );
fail( "Expecting failure" );
}
catch (PersistenceException e) {
catch (Exception e) {
//verify that an exception is thrown!
assertTyping( PersistentObjectException.class, e );
assertTyping( EntityExistsException.class, e );
}
}
);
Expand All @@ -203,9 +204,9 @@ public void testCreateExceptionWithGeneratedId(SessionFactoryScope scope) {
session.persist( nondupe );
fail( "Expecting failure" );
}
catch (PersistenceException e) {
catch (Exception e) {
//verify that an exception is thrown!
assertTyping( PersistentObjectException.class, e );
assertTyping( EntityExistsException.class, e );
}
}
);
Expand Down

0 comments on commit 42d8680

Please sign in to comment.