HHH-6697 EntityManager.persist should through EntityExistsException instead of PersistenceException (from https://github.com/hibernate/hibernate-core/pull/178)
This commit is contained in:
parent
cbec206e2f
commit
a34c99a34c
|
@ -32,7 +32,7 @@
|
|||
user.home - User's home directory
|
||||
user.dir - User's current working directory
|
||||
java.io.tmpdir - Default temp file path -->
|
||||
<diskStore path="hibernate-ehcache/target/tmp"/>
|
||||
<diskStore path="./target/tmp"/>
|
||||
|
||||
|
||||
<!--Default Cache configuration. These will applied to caches programmatically created through
|
||||
|
|
|
@ -25,6 +25,7 @@ package org.hibernate.ejb;
|
|||
|
||||
import javax.persistence.CacheRetrieveMode;
|
||||
import javax.persistence.CacheStoreMode;
|
||||
import javax.persistence.EntityExistsException;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.EntityTransaction;
|
||||
|
@ -1320,6 +1321,11 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
|
|||
handlePersistenceException( converted );
|
||||
return converted;
|
||||
}
|
||||
else if ( e instanceof org.hibernate.NonUniqueObjectException ) {
|
||||
EntityExistsException converted = new EntityExistsException( e.getMessage() );
|
||||
handlePersistenceException( converted );
|
||||
return converted;
|
||||
}
|
||||
else if ( e instanceof org.hibernate.NonUniqueResultException ) {
|
||||
NonUniqueResultException converted = new NonUniqueResultException( e.getMessage() );
|
||||
handlePersistenceException( converted );
|
||||
|
|
|
@ -24,9 +24,11 @@
|
|||
*/
|
||||
package org.hibernate.ejb.test;
|
||||
|
||||
import javax.persistence.EntityExistsException;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.FlushModeType;
|
||||
import javax.persistence.PersistenceException;
|
||||
import javax.persistence.Query;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
@ -318,7 +320,7 @@ public class EntityManagerTest extends BaseEntityManagerFunctionalTestCase {
|
|||
public void testGet() throws Exception {
|
||||
EntityManager em = getOrCreateEntityManager();
|
||||
em.getTransaction().begin();
|
||||
Item item = ( Item ) em.getReference( Item.class, "nonexistentone" );
|
||||
Item item = em.getReference( Item.class, "nonexistentone" );
|
||||
try {
|
||||
item.getDescr();
|
||||
em.getTransaction().commit();
|
||||
|
@ -391,4 +393,39 @@ public class EntityManagerTest extends BaseEntityManagerFunctionalTestCase {
|
|||
assertEquals( "MANUAL", em.getProperties().get( AvailableSettings.FLUSH_MODE ) );
|
||||
em.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPersistExisting() throws Exception {
|
||||
EntityManager em = getOrCreateEntityManager();
|
||||
em.getTransaction().begin();
|
||||
Wallet w = new Wallet();
|
||||
w.setBrand( "Lacoste" );
|
||||
w.setModel( "Minimic" );
|
||||
w.setSerial( "0100202002" );
|
||||
em.persist( w );
|
||||
w = new Wallet();
|
||||
w.setBrand( "Lacoste" );
|
||||
w.setModel( "Minimic" );
|
||||
w.setSerial( "0100202002" );
|
||||
try {
|
||||
em.persist( w );
|
||||
}
|
||||
catch ( EntityExistsException eee ) {
|
||||
//success
|
||||
if ( em.getTransaction() != null ) {
|
||||
em.getTransaction().rollback();
|
||||
}
|
||||
em.close();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
em.getTransaction().commit();
|
||||
fail( "Should have raised an exception" );
|
||||
}
|
||||
catch ( PersistenceException pe ) {
|
||||
}
|
||||
finally {
|
||||
em.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue