HHH-11426 - NullPointerException in getPersistenceUnitUtil().getIdentifier() on detached enhanced entity

This commit is contained in:
Gail Badner 2017-05-09 23:09:37 -07:00
parent d968423397
commit fd778109bc
1 changed files with 20 additions and 11 deletions

View File

@ -42,6 +42,7 @@ import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cfg.annotations.NamedEntityGraphDefinition;
import org.hibernate.ejb.HibernateEntityManagerFactory;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.ManagedEntity;
import org.hibernate.engine.spi.NamedQueryDefinition;
import org.hibernate.engine.spi.NamedQueryDefinitionBuilder;
@ -672,12 +673,18 @@ public class EntityManagerFactoryImpl implements HibernateEntityManagerFactory {
}
if ( entity instanceof HibernateProxy ) {
final HibernateProxy proxy = (HibernateProxy) entity;
return proxy.getHibernateLazyInitializer().getIdentifier();
return ((HibernateProxy) entity).getHibernateLazyInitializer().getIdentifier();
}
else if ( entity instanceof ManagedEntity ) {
final ManagedEntity enhancedEntity = (ManagedEntity) entity;
return enhancedEntity.$$_hibernate_getEntityEntry().getId();
EntityEntry entityEntry = ((ManagedEntity) entity).$$_hibernate_getEntityEntry();
if ( entityEntry != null ) {
return entityEntry.getId();
}
else {
// HHH-11426 - best effort to deal with the case of detached entities
log.debug( "javax.persistence.PersistenceUnitUtil.getIdentifier may not be able to read identifier of a detached entity" );
return getIdentifierFromPersister( entity );
}
}
else {
log.debugf(
@ -685,14 +692,16 @@ public class EntityManagerFactoryImpl implements HibernateEntityManagerFactory {
"(although Hibernate also adapts this support to its proxies); " +
"however the passed entity was not enhanced (nor a proxy).. may not be able to read identifier"
);
final Class entityClass = Hibernate.getClass( entity );
final EntityPersister persister = emf.getSessionFactory().getEntityPersister( entityClass.getName() );
if ( persister == null ) {
throw new IllegalArgumentException( entityClass + " is not an entity" );
}
//TODO does that work for @IdClass?
return persister.getIdentifier( entity );
return getIdentifierFromPersister( entity ); }
}
private Object getIdentifierFromPersister(Object entity) {
Class<?> entityClass = Hibernate.getClass( entity );
EntityPersister persister = emf.getSessionFactory().getEntityPersister( entityClass.getName() );
if ( persister == null ) {
throw new IllegalArgumentException( entityClass.getName() + " is not an entity" );
}
return persister.getIdentifier( entity, null );
}
}
}