diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/FlushVisitor.java b/hibernate-core/src/main/java/org/hibernate/event/internal/FlushVisitor.java index a506004105..4b2026aee7 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/FlushVisitor.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/FlushVisitor.java @@ -7,7 +7,7 @@ package org.hibernate.event.internal; import org.hibernate.HibernateException; -import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer; +import org.hibernate.bytecode.instrumentation.spi.LazyPropertyInitializer; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.internal.Collections; import org.hibernate.event.spi.EventSource; diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/entity/AbstractEntityTuplizer.java b/hibernate-core/src/main/java/org/hibernate/tuple/entity/AbstractEntityTuplizer.java index acac92d79a..00e7ac60b0 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/entity/AbstractEntityTuplizer.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/entity/AbstractEntityTuplizer.java @@ -14,12 +14,15 @@ import java.util.Set; import org.hibernate.EntityMode; import org.hibernate.HibernateException; import org.hibernate.MappingException; +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.bytecode.instrumentation.spi.LazyPropertyInitializer; -import org.hibernate.bytecode.spi.EntityInstrumentationMetadata; 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.PersistentAttributeInterceptor; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.event.service.spi.EventListenerRegistry; @@ -495,19 +498,32 @@ public abstract class AbstractEntityTuplizer implements EntityTuplizer { public Object[] getPropertyValues(Object entity) throws HibernateException { final int span = entityMetamodel.getPropertySpan(); final Object[] result = new Object[span]; - final EntityInstrumentationMetadata enhancementMetadata = entityMetamodel.getInstrumentationMetadata(); - final FieldInterceptor interceptor = enhancementMetadata.isInstrumented() - ? enhancementMetadata.extractInterceptor( entity ) - : null; + + LazyAttributeLoader lazyAttributeLoader = null; + FieldInterceptor fieldInterceptor = null; + if ( entity instanceof PersistentAttributeInterceptable ) { + PersistentAttributeInterceptor interceptor = ( (PersistentAttributeInterceptable) entity ).$$_hibernate_getInterceptor(); + if ( interceptor != null && interceptor instanceof LazyAttributeLoader ) { + lazyAttributeLoader = (LazyAttributeLoader) interceptor; + } + } + // Don't bother checking for FieldInterceptor if lazyAttributeLoader is non-null. + if ( lazyAttributeLoader == null && FieldInterceptionHelper.isInstrumented( entity ) ) { + fieldInterceptor = FieldInterceptionHelper.extractFieldInterceptor( entity ); + } for ( int j = 0; j < span; j++ ) { NonIdentifierAttribute property = entityMetamodel.getProperties()[j]; - if ( !property.isLazy() || interceptor == null || interceptor.isInitialized( property.getName() ) ) { - result[j] = getters[j].get( entity ); - } - else { - result[j] = LazyPropertyInitializer.UNFETCHED_PROPERTY; + boolean isInitialized = true; + if ( property.isLazy() ) { + if ( lazyAttributeLoader != null ) { + isInitialized = lazyAttributeLoader.isAttributeLoaded( property.getName() ); + } + else if ( fieldInterceptor != null ) { + isInitialized = fieldInterceptor.isInitialized( property.getName() ); + } } + result[j] = isInitialized ? getters[j].get( entity ) : LazyPropertyInitializer.UNFETCHED_PROPERTY; } return result; } diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/AdditionalDetails.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/AdditionalDetails.java new file mode 100644 index 0000000000..733c9a5b1d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/AdditionalDetails.java @@ -0,0 +1,28 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.bytecode.enhancement.lazy; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.MapsId; +import javax.persistence.OneToOne; + +/** + * @author Luis Barreiro + */ +@Entity( name = "AdditionalDetails" ) +public class AdditionalDetails { + + @Id + private Long id; + + String details; + + @OneToOne( optional = false ) + @MapsId + Post post; +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/LazyCollectionDeletedTestTask.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/LazyCollectionDeletedTestTask.java index 2e5476c0cd..9d541bb9b2 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/LazyCollectionDeletedTestTask.java +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/LazyCollectionDeletedTestTask.java @@ -12,14 +12,6 @@ import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask; -import javax.persistence.CascadeType; -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.ManyToMany; -import javax.persistence.MapsId; -import javax.persistence.OneToOne; import java.util.HashSet; import java.util.Set; @@ -29,7 +21,6 @@ import static org.junit.Assert.assertFalse; * @author Luis Barreiro */ public class LazyCollectionDeletedTestTask extends AbstractEnhancerTestTask { - private static final int CHILDREN_SIZE = 10; private Long postId; public Class[] getAnnotatedClasses() { @@ -95,43 +86,4 @@ public class LazyCollectionDeletedTestTask extends AbstractEnhancerTestTask { protected void cleanup() { } - - // --- // - - @Entity( name = "Tag" ) - public static class Tag { - - @Id - @GeneratedValue - private Long id; - - private String name; - } - - @Entity( name = "Post" ) - public static class Post { - - @Id - @GeneratedValue - private Long id; - - @ManyToMany( cascade = CascadeType.ALL ) - private Set tags; - - @OneToOne( fetch = FetchType.LAZY, mappedBy = "post", cascade = CascadeType.ALL ) - private AdditionalDetails additionalDetails; - } - - @Entity( name = "AdditionalDetails" ) - public static class AdditionalDetails { - - @Id - private Long id; - - private String details; - - @OneToOne( optional = false ) - @MapsId - private Post post; - } } \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/Post.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/Post.java new file mode 100644 index 0000000000..ef9125c84f --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/Post.java @@ -0,0 +1,34 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.bytecode.enhancement.lazy; + +import java.util.Set; +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToMany; +import javax.persistence.OneToOne; + +/** + * @author Luis Barreiro + */ +@Entity( name = "Post" ) +public class Post { + + @Id + @GeneratedValue + private Long id; + + @ManyToMany( cascade = CascadeType.ALL ) + Set tags; + + @OneToOne( fetch = FetchType.LAZY, mappedBy = "post", cascade = CascadeType.ALL ) + AdditionalDetails additionalDetails; +} + diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/Tag.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/Tag.java new file mode 100644 index 0000000000..efef2311fd --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/Tag.java @@ -0,0 +1,25 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.bytecode.enhancement.lazy; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +/** + * @author Luis Barreiro + */ +@Entity( name = "Tag" ) +public class Tag { + + @Id + @GeneratedValue + private Long id; + + String name; +} +