HHH-13380 Bytecode enhanced entities might throw LazyInitializationException from custom equals/hashcode implementations

Apparently we trigger the need to invoke equals/hashcode methods on managed entities just before the Session is attached to the BytecodeLazyAttributeInterceptor
This commit is contained in:
Sanne Grinovero 2020-08-11 20:11:10 +02:00
parent f270f688e6
commit 1d51898202
1 changed files with 15 additions and 0 deletions

View File

@ -16,11 +16,13 @@ import org.hibernate.EntityNameResolver;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer;
import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor;
import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributesMetadata;
import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.Assigned;
@ -702,12 +704,25 @@ public abstract class AbstractEntityTuplizer implements EntityTuplizer {
@Override
public final Object instantiate(Serializable id, SharedSessionContractImplementor session) {
Object result = getInstantiator().instantiate( id );
linkToSession( result, session );
if ( id != null ) {
setIdentifier( result, id, session );
}
return result;
}
protected void linkToSession(Object entity, SharedSessionContractImplementor session) {
if ( session == null ) {
return;
}
if ( entity instanceof PersistentAttributeInterceptable ) {
final BytecodeLazyAttributeInterceptor interceptor = getEntityMetamodel().getBytecodeEnhancementMetadata().extractLazyInterceptor( entity );
if ( interceptor != null ) {
interceptor.setSession( session );
}
}
}
@Override
public final Object instantiate() throws HibernateException {
return instantiate( null, null );