HHH-11294 - NPE in org.hibernate.jpa.internal.util.PersistenceUtilHelper.isInitialized(PersistentAttributeInterceptable)

This commit is contained in:
Craig Andrews 2016-11-30 14:16:13 -05:00 committed by barreiro
parent 0908b2d9a6
commit 381cc73cb3
No known key found for this signature in database
GPG Key ID: B56FFB7502C31F42
2 changed files with 38 additions and 5 deletions

View File

@ -20,9 +20,11 @@ import java.util.WeakHashMap;
import javax.persistence.spi.LoadState;
import org.hibernate.HibernateException;
import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoader;
import org.hibernate.bytecode.instrumentation.internal.FieldInterceptionHelper;
import org.hibernate.bytecode.instrumentation.spi.FieldInterceptor;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
@ -83,6 +85,10 @@ public final class PersistenceUtilHelper {
final boolean isInitialized = interceptor == null || interceptor.isInitialized();
return isInitialized ? LoadState.LOADED : LoadState.NOT_LOADED;
}
else if ( reference instanceof PersistentAttributeInterceptable ) {
final boolean isInitialized = isInitialized( (PersistentAttributeInterceptable) reference );
return isInitialized ? LoadState.LOADED : LoadState.NOT_LOADED;
}
else if ( reference instanceof PersistentCollection ) {
final boolean isInitialized = ( (PersistentCollection) reference ).wasInitialized();
return isInitialized ? LoadState.LOADED : LoadState.NOT_LOADED;
@ -92,6 +98,11 @@ public final class PersistenceUtilHelper {
}
}
private static boolean isInitialized(PersistentAttributeInterceptable interceptable) {
LazyAttributeLoader interceptor = (LazyAttributeLoader) interceptable.$$_hibernate_getInterceptor();
return interceptor == null || interceptor.isUninitialized();
}
/**
* Is the given attribute (by name) loaded? This form must take care to not access the attribute (trigger
* initialization).

View File

@ -8,17 +8,20 @@ package org.hibernate.jpa.test.util;
import javax.persistence.spi.LoadState;
import org.junit.Test;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
import org.hibernate.jpa.internal.util.PersistenceUtilHelper;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Tests for HHH-5094 and HHH-5334
*
* @author Hardy Ferentschik
*/
public class PersistenceUtilHelperTest{
public class PersistenceUtilHelperTest {
private final PersistenceUtilHelper.MetadataCache cache = new PersistenceUtilHelper.MetadataCache();
public static class FieldAccessBean extends FieldAccessBeanBase {
@ -93,8 +96,27 @@ public class PersistenceUtilHelperTest{
public void testIsLoadedWithReferencePrivateMethod() {
assertEquals(
LoadState.UNKNOWN,
PersistenceUtilHelper.isLoadedWithReference(
new MethodAccessBean(), "privateAccessPropertyValue", cache
PersistenceUtilHelper.isLoadedWithReference( new MethodAccessBean(), "privateAccessPropertyValue", cache )
);
}
@Test
public void testIsLoadedWithNullInterceptor() {
assertEquals(
LoadState.LOADED,
PersistenceUtilHelper.isLoaded(
new PersistentAttributeInterceptable() {
@Override
public PersistentAttributeInterceptor $$_hibernate_getInterceptor() {
return null;
}
@Override
public void $$_hibernate_setInterceptor(PersistentAttributeInterceptor interceptor) {
}
}
)
);
}