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 javax.persistence.spi.LoadState;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoader;
import org.hibernate.bytecode.instrumentation.internal.FieldInterceptionHelper; import org.hibernate.bytecode.instrumentation.internal.FieldInterceptionHelper;
import org.hibernate.bytecode.instrumentation.spi.FieldInterceptor; import org.hibernate.bytecode.instrumentation.spi.FieldInterceptor;
import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.proxy.HibernateProxy; import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer; import org.hibernate.proxy.LazyInitializer;
@ -83,6 +85,10 @@ public final class PersistenceUtilHelper {
final boolean isInitialized = interceptor == null || interceptor.isInitialized(); final boolean isInitialized = interceptor == null || interceptor.isInitialized();
return isInitialized ? LoadState.LOADED : LoadState.NOT_LOADED; 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 ) { else if ( reference instanceof PersistentCollection ) {
final boolean isInitialized = ( (PersistentCollection) reference ).wasInitialized(); final boolean isInitialized = ( (PersistentCollection) reference ).wasInitialized();
return isInitialized ? LoadState.LOADED : LoadState.NOT_LOADED; 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 * Is the given attribute (by name) loaded? This form must take care to not access the attribute (trigger
* initialization). * initialization).

View File

@ -8,11 +8,14 @@ package org.hibernate.jpa.test.util;
import javax.persistence.spi.LoadState; 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.hibernate.jpa.internal.util.PersistenceUtilHelper;
import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
/** /**
* Tests for HHH-5094 and HHH-5334 * Tests for HHH-5094 and HHH-5334
* *
@ -93,8 +96,27 @@ public class PersistenceUtilHelperTest{
public void testIsLoadedWithReferencePrivateMethod() { public void testIsLoadedWithReferencePrivateMethod() {
assertEquals( assertEquals(
LoadState.UNKNOWN, LoadState.UNKNOWN,
PersistenceUtilHelper.isLoadedWithReference( PersistenceUtilHelper.isLoadedWithReference( new MethodAccessBean(), "privateAccessPropertyValue", cache )
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) {
}
}
) )
); );
} }