HHH-11762 - PersistenceUnitUtilImpl#getIdentifier throws MappingException for non-entity

This commit is contained in:
Gail Badner 2017-05-19 18:28:44 -07:00
parent ec165296c6
commit 379e32e3f3
2 changed files with 34 additions and 3 deletions

View File

@ -11,6 +11,7 @@ import javax.persistence.PersistenceUnitUtil;
import javax.persistence.spi.LoadState;
import org.hibernate.Hibernate;
import org.hibernate.MappingException;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.ManagedEntity;
import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -92,9 +93,15 @@ public class PersistenceUnitUtilImpl implements PersistenceUnitUtil, Serializabl
private Object getIdentifierFromPersister(Object entity) {
Class<?> entityClass = Hibernate.getClass( entity );
EntityPersister persister = sessionFactory.getMetamodel().entityPersister( entityClass );
if ( persister == null ) {
throw new IllegalArgumentException( entityClass.getName() + " is not an entity" );
final EntityPersister persister;
try {
persister = sessionFactory.getMetamodel().entityPersister( entityClass );
if ( persister == null ) {
throw new IllegalArgumentException( entityClass.getName() + " is not an entity" );
}
}
catch (MappingException ex) {
throw new IllegalArgumentException( entityClass.getName() + " is not an entity", ex );
}
return persister.getIdentifier( entity, null );
}

View File

@ -15,6 +15,8 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.fail;
/**
* @author Steve Ebersole
*/
@ -51,6 +53,28 @@ public class GetIdentifierTest extends BaseEntityManagerFunctionalTestCase {
entityManager.close();
}
@Test
public void getIdentifierOfNonEntityTest() {
try {
entityManagerFactory().getPersistenceUnitUtil().getIdentifier( this );
fail( "should have thrown IllegalArgumentException" );
}
catch (IllegalArgumentException ex) {
// expected
}
}
@Test
public void getIdentifierOfNullTest() {
try {
entityManagerFactory().getPersistenceUnitUtil().getIdentifier( null );
fail( "should have thrown IllegalArgumentException" );
}
catch (IllegalArgumentException ex) {
// expected
}
}
private NestedLegacyEntity createExisitingNestedLegacyEntity() {
ModernEntity modernEntity = new ModernEntity();