diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/util/PersistenceUtilHelper.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/util/PersistenceUtilHelper.java index 18946e3897..1dc5be6ef6 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/util/PersistenceUtilHelper.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/util/PersistenceUtilHelper.java @@ -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). diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/util/PersistenceUtilHelperTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/util/PersistenceUtilHelperTest.java index 086e78197e..0c6081038b 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/util/PersistenceUtilHelperTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/util/PersistenceUtilHelperTest.java @@ -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) { + + } + } ) ); }