diff --git a/hibernate-core/src/main/java/org/hibernate/Hibernate.java b/hibernate-core/src/main/java/org/hibernate/Hibernate.java index 3cbbac50ff..91fe9421e9 100644 --- a/hibernate-core/src/main/java/org/hibernate/Hibernate.java +++ b/hibernate-core/src/main/java/org/hibernate/Hibernate.java @@ -37,15 +37,16 @@ import org.hibernate.collection.spi.LazyInitializable; import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable; import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable; +import static org.hibernate.proxy.HibernateProxy.extractLazyInitializer; /** * Various utility functions for working with proxies and lazy collection references. *

* Operations like {@link #isInitialized(Object)} and {@link #initialize(Object)} are * of general purpose. But {@link #createDetachedProxy(SessionFactory, Class, Object)} - * and {@link CollectionInterface#createDetachedProxy(SessionFactory, Class, Object)} - * are intended for use by generic code that must materialize an "amputated" graph of - * Hibernate entities. (For example, a library which deserializes entities from JSON.) + * and {@link CollectionInterface#createDetachedInstance()} are intended for use by + * generic code that must materialize an "amputated" graph of Hibernate entities. + * (For example, a library which deserializes entities from JSON.) *

* Lazy fetching of a {@linkplain jakarta.persistence.OneToOne one to one} or * {@linkplain jakarta.persistence.ManyToOne many to one} association requires special @@ -126,7 +127,7 @@ public final class Hibernate { return; } - final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( proxy ); + final LazyInitializer lazyInitializer = extractLazyInitializer( proxy ); if ( lazyInitializer != null ) { lazyInitializer.initialize(); } @@ -134,7 +135,8 @@ public final class Hibernate { ( (LazyInitializable) proxy ).forceInitialization(); } else if ( isPersistentAttributeInterceptable( proxy ) ) { - final PersistentAttributeInterceptor interceptor = asPersistentAttributeInterceptable( proxy ).$$_hibernate_getInterceptor(); + final PersistentAttributeInterceptor interceptor = + asPersistentAttributeInterceptable( proxy ).$$_hibernate_getInterceptor(); if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor ) { ( (EnhancementAsProxyLazinessInterceptor) interceptor ).forceInitialize( proxy, null ); } @@ -150,12 +152,13 @@ public final class Hibernate { * @return true if the argument is already initialized, or is not a proxy or collection */ public static boolean isInitialized(Object proxy) { - final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( proxy ); + final LazyInitializer lazyInitializer = extractLazyInitializer( proxy ); if ( lazyInitializer != null ) { return !lazyInitializer.isUninitialized(); } else if ( isPersistentAttributeInterceptable( proxy ) ) { - final PersistentAttributeInterceptor interceptor = asPersistentAttributeInterceptable( proxy ).$$_hibernate_getInterceptor(); + final PersistentAttributeInterceptor interceptor = + asPersistentAttributeInterceptable( proxy ).$$_hibernate_getInterceptor(); if (interceptor instanceof EnhancementAsProxyLazinessInterceptor) { return ( (EnhancementAsProxyLazinessInterceptor) interceptor ).isInitialized(); } @@ -294,7 +297,7 @@ public final class Hibernate { @SuppressWarnings("unchecked") public static Class getClass(T proxy) { Class result; - final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( proxy ); + final LazyInitializer lazyInitializer = extractLazyInitializer( proxy ); if ( lazyInitializer != null ) { result = lazyInitializer .getImplementation() @@ -334,7 +337,7 @@ public final class Hibernate { */ public static boolean isPropertyInitialized(Object proxy, String propertyName) { final Object entity; - final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( proxy ); + final LazyInitializer lazyInitializer = extractLazyInitializer( proxy ); if ( lazyInitializer != null ) { if ( lazyInitializer.isUninitialized() ) { return false; @@ -369,7 +372,7 @@ public final class Hibernate { * uninitialized proxy that is not associated with an open session. */ public static Object unproxy(Object proxy) { - final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( proxy ); + final LazyInitializer lazyInitializer = extractLazyInitializer( proxy ); if ( lazyInitializer != null ) { return lazyInitializer.getImplementation(); } @@ -397,7 +400,7 @@ public final class Hibernate { /** * Obtain a detached, uninitialized reference (a proxy) for a persistent entity with * the given identifier. - * + *

* The returned proxy is not associated with any session, and cannot be initialized * by calling {@link #initialize(Object)}. It can be used to represent a reference to * the entity when working with a detached object graph. @@ -441,7 +444,7 @@ public final class Hibernate { /** * Obtain a detached, uninitialized persistent collection of the type represented * by this object. - * + *

* The returned wrapper object is not associated with any session, and cannot be * initialized by calling {@link #initialize(Object)}. It can be used to represent * an uninitialized collection when working with a detached object graph. diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/HibernatePersistenceProvider.java b/hibernate-core/src/main/java/org/hibernate/jpa/HibernatePersistenceProvider.java index 9994f4aeb4..61cc5450e5 100644 --- a/hibernate-core/src/main/java/org/hibernate/jpa/HibernatePersistenceProvider.java +++ b/hibernate-core/src/main/java/org/hibernate/jpa/HibernatePersistenceProvider.java @@ -189,8 +189,8 @@ public class HibernatePersistenceProvider implements PersistenceProvider { return PersistenceUtilHelper.isLoadedWithReference( proxy, property, cache ); } @Override - public LoadState isLoaded(Object o) { - return PersistenceUtilHelper.isLoaded(o); + public LoadState isLoaded(Object object) { + return PersistenceUtilHelper.getLoadState( object ); } }; diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/internal/PersistenceUnitUtilImpl.java b/hibernate-core/src/main/java/org/hibernate/jpa/internal/PersistenceUnitUtilImpl.java index 53ad51f54e..2cfbe054cd 100644 --- a/hibernate-core/src/main/java/org/hibernate/jpa/internal/PersistenceUnitUtilImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/jpa/internal/PersistenceUnitUtilImpl.java @@ -8,7 +8,6 @@ package org.hibernate.jpa.internal; import java.io.Serializable; import jakarta.persistence.PersistenceUnitUtil; -import jakarta.persistence.spi.LoadState; import org.hibernate.Hibernate; import org.hibernate.MappingException; @@ -16,13 +15,17 @@ import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.jpa.internal.util.PersistenceUtilHelper; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.proxy.HibernateProxy; import org.hibernate.proxy.LazyInitializer; import org.jboss.logging.Logger; +import static jakarta.persistence.spi.LoadState.NOT_LOADED; import static org.hibernate.engine.internal.ManagedTypeHelper.asManagedEntity; import static org.hibernate.engine.internal.ManagedTypeHelper.isManagedEntity; +import static org.hibernate.jpa.internal.util.PersistenceUtilHelper.getLoadState; +import static org.hibernate.jpa.internal.util.PersistenceUtilHelper.isLoadedWithReference; +import static org.hibernate.jpa.internal.util.PersistenceUtilHelper.isLoadedWithoutReference; +import static org.hibernate.proxy.HibernateProxy.extractLazyInitializer; /** * @author Steve Ebersole @@ -41,19 +44,13 @@ public class PersistenceUnitUtilImpl implements PersistenceUnitUtil, Serializabl public boolean isLoaded(Object entity, String attributeName) { // added log message to help with HHH-7454, if state == LoadState,NOT_LOADED, returning true or false is not accurate. log.debug( "PersistenceUnitUtil#isLoaded is not always accurate; consider using EntityManager#contains instead" ); - LoadState state = PersistenceUtilHelper.isLoadedWithoutReference( entity, attributeName, cache ); - if ( state == LoadState.LOADED ) { - return true; - } - else if ( state == LoadState.NOT_LOADED ) { - return false; - } - else { - return PersistenceUtilHelper.isLoadedWithReference( - entity, - attributeName, - cache - ) != LoadState.NOT_LOADED; + switch ( isLoadedWithoutReference( entity, attributeName, cache ) ) { + case LOADED: + return true; + case NOT_LOADED: + return false; + default: + return isLoadedWithReference( entity, attributeName, cache ) != NOT_LOADED; } } @@ -61,7 +58,7 @@ public class PersistenceUnitUtilImpl implements PersistenceUnitUtil, Serializabl public boolean isLoaded(Object entity) { // added log message to help with HHH-7454, if state == LoadState,NOT_LOADED, returning true or false is not accurate. log.debug( "PersistenceUnitUtil#isLoaded is not always accurate; consider using EntityManager#contains instead" ); - return PersistenceUtilHelper.isLoaded( entity ) != LoadState.NOT_LOADED; + return getLoadState( entity ) != NOT_LOADED; } @Override @@ -70,12 +67,12 @@ public class PersistenceUnitUtilImpl implements PersistenceUnitUtil, Serializabl throw new IllegalArgumentException( "Passed entity cannot be null" ); } - final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( entity ); + final LazyInitializer lazyInitializer = extractLazyInitializer( entity ); if ( lazyInitializer != null ) { return lazyInitializer.getInternalIdentifier(); } else if ( isManagedEntity( entity ) ) { - EntityEntry entityEntry = asManagedEntity( entity ).$$_hibernate_getEntityEntry(); + final EntityEntry entityEntry = asManagedEntity( entity ).$$_hibernate_getEntityEntry(); if ( entityEntry != null ) { return entityEntry.getId(); } @@ -96,7 +93,7 @@ public class PersistenceUnitUtilImpl implements PersistenceUnitUtil, Serializabl } private Object getIdentifierFromPersister(Object entity) { - Class entityClass = Hibernate.getClass( entity ); + final Class entityClass = Hibernate.getClass( entity ); final EntityPersister persister; try { persister = sessionFactory.getRuntimeMetamodels() diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/internal/util/PersistenceUtilHelper.java b/hibernate-core/src/main/java/org/hibernate/jpa/internal/util/PersistenceUtilHelper.java index 5490ec9950..19ac831fe7 100644 --- a/hibernate-core/src/main/java/org/hibernate/jpa/internal/util/PersistenceUtilHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/jpa/internal/util/PersistenceUtilHelper.java @@ -11,7 +11,6 @@ import java.io.Serializable; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.security.AccessController; import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.HashMap; @@ -23,6 +22,7 @@ import jakarta.persistence.spi.LoadState; import org.hibernate.HibernateException; import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor; +import org.hibernate.collection.spi.LazyInitializable; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.internal.util.ReflectHelper; @@ -30,8 +30,13 @@ import org.hibernate.internal.util.securitymanager.SystemSecurityManager; import org.hibernate.proxy.HibernateProxy; import org.hibernate.proxy.LazyInitializer; +import static jakarta.persistence.spi.LoadState.LOADED; +import static jakarta.persistence.spi.LoadState.NOT_LOADED; +import static jakarta.persistence.spi.LoadState.UNKNOWN; +import static java.security.AccessController.doPrivileged; import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable; import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable; +import static org.hibernate.proxy.HibernateProxy.extractLazyInitializer; /** * Central delegate for handling calls from: