From 08747fc2f4b33414ef05ac039b67e1d33c14db5f Mon Sep 17 00:00:00 2001 From: John Lin Date: Mon, 12 Nov 2018 17:32:07 +0800 Subject: [PATCH] HHH-13094 - Respect @Any.fetch setting to FetchType.EAGER --- .../source/internal/hbm/ModelBinder.java | 3 + .../hbm/SingularAttributeSourceAnyImpl.java | 5 ++ .../model/source/spi/AnyMappingSource.java | 4 + .../org/hibernate/cfg/AnnotationBinder.java | 4 +- .../java/org/hibernate/cfg/BinderHelper.java | 2 + .../cfg/annotations/CollectionBinder.java | 1 + .../main/java/org/hibernate/mapping/Any.java | 15 +++- .../main/java/org/hibernate/type/AnyType.java | 12 +-- .../java/org/hibernate/type/TypeFactory.java | 23 ++++- .../test/annotations/any/AnyTest.java | 89 ++++++++++++++++--- .../test/annotations/any/LazyPropertySet.java | 70 +++++++++++++++ .../test/annotations/any/PropertyList.java | 2 +- .../test/annotations/any/PropertySet.java | 2 +- .../hibernate/test/any/AnyEagerHbmTest.java | 60 +++++++++++++ .../hibernate/test/any/AnyLazyHbmTest.java | 73 +++++++++++++++ .../test/any/AnyTestEagerPropertySet.hbm.xml | 27 ++++++ .../test/any/AnyTestLazyPropertySet.hbm.xml | 27 ++++++ 17 files changed, 394 insertions(+), 25 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/any/LazyPropertySet.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/any/AnyEagerHbmTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/any/AnyLazyHbmTest.java create mode 100644 hibernate-core/src/test/resources/org/hibernate/test/any/AnyTestEagerPropertySet.hbm.xml create mode 100644 hibernate-core/src/test/resources/org/hibernate/test/any/AnyTestLazyPropertySet.hbm.xml diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java index 2c7ec3ee1f..5c7960314c 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java @@ -2343,6 +2343,9 @@ public class ModelBinder { Any anyBinding, final AttributeRole attributeRole, AttributePath attributePath) { + + anyBinding.setLazy( anyMapping.isLazy() ); + final TypeResolution keyTypeResolution = resolveType( sourceDocument, anyMapping.getKeySource().getTypeSource() diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/SingularAttributeSourceAnyImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/SingularAttributeSourceAnyImpl.java index 216ca91635..fb3a0e4ad9 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/SingularAttributeSourceAnyImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/SingularAttributeSourceAnyImpl.java @@ -268,4 +268,9 @@ public class SingularAttributeSourceAnyImpl public String getCascadeStyleName() { return jaxbAnyMapping.getCascade(); } + + @Override + public boolean isLazy() { + return isBytecodeLazy(); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/source/spi/AnyMappingSource.java b/hibernate-core/src/main/java/org/hibernate/boot/model/source/spi/AnyMappingSource.java index b58bc5d244..deaaec6572 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/source/spi/AnyMappingSource.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/source/spi/AnyMappingSource.java @@ -15,4 +15,8 @@ package org.hibernate.boot.model.source.spi; public interface AnyMappingSource { AnyDiscriminatorSource getDiscriminatorSource(); AnyKeySource getKeySource(); + + default boolean isLazy() { + return true; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java index 28078bd74f..6311bf8afa 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -3272,12 +3272,14 @@ public final class AnnotationBinder { + BinderHelper.getPath( propertyHolder, inferredData ) ); } + boolean lazy = ( anyAnn.fetch() == FetchType.LAZY ); Any value = BinderHelper.buildAnyValue( anyAnn.metaDef(), columns, anyAnn.metaColumn(), inferredData, cascadeOnDelete, + lazy, nullability, propertyHolder, entityBinder, @@ -3289,7 +3291,7 @@ public final class AnnotationBinder { binder.setName( inferredData.getPropertyName() ); binder.setValue( value ); - binder.setLazy( anyAnn.fetch() == FetchType.LAZY ); + binder.setLazy( lazy ); //binder.setCascade(cascadeStrategy); if ( isIdentifierMapper ) { binder.setInsertable( false ); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java b/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java index 2ade91196c..a50a7efed2 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java @@ -941,6 +941,7 @@ public class BinderHelper { javax.persistence.Column metaColumn, PropertyData inferredData, boolean cascadeOnDelete, + boolean lazy, Nullability nullability, PropertyHolder propertyHolder, EntityBinder entityBinder, @@ -950,6 +951,7 @@ public class BinderHelper { Any value = new Any( context, columns[0].getTable() ); AnyMetaDef metaAnnDef = inferredData.getProperty().getAnnotation( AnyMetaDef.class ); + value.setLazy( lazy ); if ( metaAnnDef != null ) { //local has precedence over general and can be mapped for future reference if named bindAnyMetaDefs( inferredData.getProperty(), context ); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java index 0945f20e00..4480d040dc 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java @@ -1494,6 +1494,7 @@ public abstract class CollectionBinder { anyAnn.metaColumn(), inferredData, cascadeDeleteEnabled, + anyAnn.fetch() == FetchType.LAZY, Nullability.NO_CONSTRAINT, propertyHolder, new EntityBinder(), diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Any.java b/hibernate-core/src/main/java/org/hibernate/mapping/Any.java index 297277e72e..c5550deb56 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Any.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Any.java @@ -24,6 +24,7 @@ public class Any extends SimpleValue { private String identifierTypeName; private String metaTypeName = "string"; private Map metaValues; + private boolean lazy = true; /** * @deprecated Use {@link Any#Any(MetadataBuildingContext, Table)} instead. @@ -50,7 +51,8 @@ public class Any extends SimpleValue { return getMetadata().getTypeResolver().getTypeFactory().any( metaValues == null ? metaType : new MetaType( metaValues, metaType ), - getMetadata().getTypeResolver().heuristicType( identifierTypeName ) + getMetadata().getTypeResolver().heuristicType( identifierTypeName ), + isLazy() ); } @@ -72,6 +74,14 @@ public class Any extends SimpleValue { this.metaValues = metaValues; } + public boolean isLazy() { + return lazy; + } + + public void setLazy(boolean lazy) { + this.lazy = lazy; + } + public void setTypeUsingReflection(String className, String propertyName) throws MappingException { } @@ -89,6 +99,7 @@ public class Any extends SimpleValue { return super.isSame( other ) && Objects.equals( identifierTypeName, other.identifierTypeName ) && Objects.equals( metaTypeName, other.metaTypeName ) - && Objects.equals( metaValues, other.metaValues ); + && Objects.equals( metaValues, other.metaValues ) + && lazy == other.lazy; } } diff --git a/hibernate-core/src/main/java/org/hibernate/type/AnyType.java b/hibernate-core/src/main/java/org/hibernate/type/AnyType.java index 2a0af5373b..0b51612358 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/AnyType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/AnyType.java @@ -47,18 +47,20 @@ public class AnyType extends AbstractType implements CompositeType, AssociationT private final TypeFactory.TypeScope scope; private final Type identifierType; private final Type discriminatorType; + private final boolean eager; /** * Intended for use only from legacy {@link ObjectType} type definition */ protected AnyType(Type discriminatorType, Type identifierType) { - this( null, discriminatorType, identifierType ); + this( null, discriminatorType, identifierType, true ); } - public AnyType(TypeFactory.TypeScope scope, Type discriminatorType, Type identifierType) { + public AnyType(TypeFactory.TypeScope scope, Type discriminatorType, Type identifierType, boolean lazy) { this.scope = scope; this.discriminatorType = discriminatorType; this.identifierType = identifierType; + this.eager = !lazy; } public Type getIdentifierType() { @@ -266,7 +268,7 @@ public class AnyType extends AbstractType implements CompositeType, AssociationT throws HibernateException { return entityName==null || id==null ? null - : session.internalLoad( entityName, id, false, false ); + : session.internalLoad( entityName, id, eager, false ); } @Override @@ -319,7 +321,7 @@ public class AnyType extends AbstractType implements CompositeType, AssociationT @Override public Object assemble(Serializable cached, SharedSessionContractImplementor session, Object owner) throws HibernateException { final ObjectTypeCacheEntry e = (ObjectTypeCacheEntry) cached; - return e == null ? null : session.internalLoad( e.entityName, e.id, false, false ); + return e == null ? null : session.internalLoad( e.entityName, e.id, eager, false ); } @Override @@ -348,7 +350,7 @@ public class AnyType extends AbstractType implements CompositeType, AssociationT else { final String entityName = session.bestGuessEntityName( original ); final Serializable id = ForeignKeys.getEntityIdentifierIfNotUnsaved( entityName, original, session ); - return session.internalLoad( entityName, id, false, false ); + return session.internalLoad( entityName, id, eager, false ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/type/TypeFactory.java b/hibernate-core/src/main/java/org/hibernate/type/TypeFactory.java index f18866cb69..b6ee8c97a9 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/TypeFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/type/TypeFactory.java @@ -401,7 +401,28 @@ public final class TypeFactory implements Serializable { // any type builder ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + /** + * Get the AnyType with the specified parameters. + * + * @param metaType meta type + * @param identifierType identifier type + * @return AnyType + * @deprecated use {@link TypeFactory#any(Type, Type, boolean)} instead + */ + @Deprecated public Type any(Type metaType, Type identifierType) { - return new AnyType( typeScope, metaType, identifierType ); + return any( metaType, identifierType, true ); + } + + /** + * Get the AnyType with the specified parameters. + * + * @param metaType meta type + * @param identifierType identifier type + * @param lazy is teh underlying proeprty lazy + * @return AnyType + */ + public Type any(Type metaType, Type identifierType, boolean lazy) { + return new AnyType( typeScope, metaType, identifierType, lazy ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/any/AnyTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/AnyTest.java index ee88849674..0a8f6bbcec 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/any/AnyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/AnyTest.java @@ -6,18 +6,27 @@ */ package org.hibernate.test.annotations.any; -import org.junit.Test; - -import org.hibernate.Query; +import org.hibernate.LazyInitializationException; import org.hibernate.Session; import org.hibernate.Transaction; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.query.Query; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public class AnyTest extends BaseCoreFunctionalTestCase { + + @Override + protected boolean isCleanupTestDataRequired() { + return true; + } + @Test public void testDefaultAnyAssociation() { Session s = openSession(); @@ -26,13 +35,13 @@ public class AnyTest extends BaseCoreFunctionalTestCase { PropertySet set1 = new PropertySet( "string" ); Property property = new StringProperty( "name", "Alex" ); set1.setSomeProperty( property ); - set1.addGeneratedProperty( property ); + set1.addGeneralProperty( property ); s.save( set1 ); PropertySet set2 = new PropertySet( "integer" ); property = new IntegerProperty( "age", 33 ); set2.setSomeProperty( property ); - set2.addGeneratedProperty( property ); + set2.addGeneralProperty( property ); s.save( set2 ); s.flush(); @@ -116,10 +125,10 @@ public class AnyTest extends BaseCoreFunctionalTestCase { list.setSomeProperty( longProperty ); - list.addGeneratedProperty( stringProperty ); - list.addGeneratedProperty( integerProperty ); - list.addGeneratedProperty( longProperty ); - list.addGeneratedProperty( charProp ); + list.addGeneralProperty( stringProperty ); + list.addGeneralProperty( integerProperty ); + list.addGeneralProperty( longProperty ); + list.addGeneralProperty( charProp ); s.save( list ); @@ -151,6 +160,57 @@ public class AnyTest extends BaseCoreFunctionalTestCase { s.close(); } + @Test + public void testFetchEager() { + doInHibernate( this::sessionFactory, s -> { + PropertySet set = new PropertySet( "string" ); + Property property = new StringProperty( "name", "Alex" ); + set.setSomeProperty( property ); + s.save( set ); + } ); + + PropertySet result = doInHibernate( this::sessionFactory, s -> { + return s.createQuery( "select s from PropertySet s where name = :name", PropertySet.class ) + .setParameter( "name", "string" ) + .getSingleResult(); + } ); + + assertNotNull( result ); + assertNotNull( result.getSomeProperty() ); + assertTrue( result.getSomeProperty() instanceof StringProperty ); + assertEquals( "Alex", result.getSomeProperty().asString() ); + } + + @Test + public void testFetchLazy() { + doInHibernate( this::sessionFactory, s -> { + LazyPropertySet set = new LazyPropertySet( "string" ); + Property property = new StringProperty( "name", "Alex" ); + set.setSomeProperty( property ); + s.save( set ); + } ); + + LazyPropertySet result = doInHibernate( this::sessionFactory, s -> { + return s.createQuery( "select s from LazyPropertySet s where name = :name", LazyPropertySet.class ) + .setParameter( "name", "string" ) + .getSingleResult(); + } ); + + assertNotNull( result ); + assertNotNull( result.getSomeProperty() ); + + try { + result.getSomeProperty().asString(); + fail( "should not get the property string after session closed." ); + } + catch (LazyInitializationException e) { + // expected + } + catch (Exception e) { + fail( "should not throw exception other than LazyInitializationException." ); + } + } + @Override protected Class[] getAnnotatedClasses() { return new Class[] { @@ -158,6 +218,7 @@ public class AnyTest extends BaseCoreFunctionalTestCase { IntegerProperty.class, LongProperty.class, PropertySet.class, + LazyPropertySet.class, PropertyMap.class, PropertyList.class, CharProperty.class @@ -172,8 +233,8 @@ public class AnyTest extends BaseCoreFunctionalTestCase { } // Simply having this orm.xml file in the classpath reproduces HHH-4261. - @Override - protected String[] getXmlFiles() { - return new String[] { "org/hibernate/test/annotations/any/orm.xml" }; - } + @Override + protected String[] getXmlFiles() { + return new String[] { "org/hibernate/test/annotations/any/orm.xml" }; + } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/any/LazyPropertySet.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/LazyPropertySet.java new file mode 100644 index 0000000000..f29b5a6552 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/LazyPropertySet.java @@ -0,0 +1,70 @@ +/* + * 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.annotations.any; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.Table; + +import org.hibernate.annotations.Any; +import org.hibernate.annotations.AnyMetaDef; +import org.hibernate.annotations.Cascade; +import org.hibernate.annotations.CascadeType; +import org.hibernate.annotations.MetaValue; + +@Entity +@Table( name = "lazy_property_set" ) +public class LazyPropertySet { + private Integer id; + private String name; + private Property someProperty; + + public LazyPropertySet() { + super(); + } + + public LazyPropertySet(String name) { + this.name = name; + } + + @Id + @GeneratedValue + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Any( metaColumn = @Column( name = "property_type" ), fetch = FetchType.LAZY ) + @Cascade( value = { CascadeType.ALL } ) + @AnyMetaDef( idType = "integer", metaType = "string", metaValues = { + @MetaValue( value = "S", targetEntity = StringProperty.class ), + @MetaValue( value = "I", targetEntity = IntegerProperty.class ) + } ) + @JoinColumn( name = "property_id" ) + public Property getSomeProperty() { + return someProperty; + } + + public void setSomeProperty(Property someProperty) { + this.someProperty = someProperty; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/any/PropertyList.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/PropertyList.java index bb9177fdcd..bbedcd2cd9 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/any/PropertyList.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/PropertyList.java @@ -84,7 +84,7 @@ public class PropertyList { this.someProperty = someProperty; } - public void addGeneratedProperty(T property) { + public void addGeneralProperty(T property) { this.generalProperties.add( property ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/any/PropertySet.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/PropertySet.java index 4286908789..0d7f19bf6b 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/any/PropertySet.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/PropertySet.java @@ -89,7 +89,7 @@ public class PropertySet { this.someProperty = someProperty; } - public void addGeneratedProperty(Property property) { + public void addGeneralProperty(Property property) { this.generalProperties.add( property ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/any/AnyEagerHbmTest.java b/hibernate-core/src/test/java/org/hibernate/test/any/AnyEagerHbmTest.java new file mode 100644 index 0000000000..91626534b7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/any/AnyEagerHbmTest.java @@ -0,0 +1,60 @@ +/* + * 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.any; + +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.test.annotations.any.*; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class AnyEagerHbmTest extends BaseCoreFunctionalTestCase { + + @Override + protected boolean isCleanupTestDataRequired() { + return true; + } + + @Test + public void testFetchEager() { + doInHibernate( this::sessionFactory, s -> { + org.hibernate.test.annotations.any.PropertySet set = new org.hibernate.test.annotations.any.PropertySet( "string" ); + Property property = new StringProperty( "name", "Alex" ); + set.setSomeProperty( property ); + s.save( set ); + } ); + + org.hibernate.test.annotations.any.PropertySet result = doInHibernate( this::sessionFactory, s -> { + return s.createQuery( "select s from PropertySet s where name = :name", org.hibernate.test.annotations.any.PropertySet.class ) + .setParameter( "name", "string" ) + .getSingleResult(); + } ); + + assertNotNull( result ); + assertNotNull( result.getSomeProperty() ); + assertTrue( result.getSomeProperty() instanceof StringProperty ); + assertEquals( "Alex", result.getSomeProperty().asString() ); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + StringProperty.class, + IntegerProperty.class, + }; + } + + @Override + public String[] getMappings() { + return new String[] { + "any/AnyTestEagerPropertySet.hbm.xml" + }; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/any/AnyLazyHbmTest.java b/hibernate-core/src/test/java/org/hibernate/test/any/AnyLazyHbmTest.java new file mode 100644 index 0000000000..1a5457c57a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/any/AnyLazyHbmTest.java @@ -0,0 +1,73 @@ +/* + * 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.any; + +import org.hibernate.LazyInitializationException; + +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.test.annotations.any.IntegerProperty; +import org.hibernate.test.annotations.any.LazyPropertySet; +import org.hibernate.test.annotations.any.Property; +import org.hibernate.test.annotations.any.StringProperty; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +public class AnyLazyHbmTest extends BaseCoreFunctionalTestCase { + + @Override + protected boolean isCleanupTestDataRequired() { + return true; + } + + @Test + public void testFetchLazy() { + doInHibernate( this::sessionFactory, s -> { + LazyPropertySet set = new LazyPropertySet( "string" ); + Property property = new StringProperty( "name", "Alex" ); + set.setSomeProperty( property ); + s.save( set ); + } ); + + LazyPropertySet result = doInHibernate( this::sessionFactory, s -> { + return s.createQuery( "select s from LazyPropertySet s where name = :name", LazyPropertySet.class ) + .setParameter( "name", "string" ) + .getSingleResult(); + } ); + + assertNotNull( result ); + assertNotNull( result.getSomeProperty() ); + + try { + result.getSomeProperty().asString(); + fail( "should not get the property string after session closed." ); + } + catch (LazyInitializationException e) { + // expected + } + catch (Exception e) { + fail( "should not throw exception other than LazyInitializationException." ); + } + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + StringProperty.class, + IntegerProperty.class, + }; + } + + @Override + public String[] getMappings() { + return new String[] { + "any/AnyTestLazyPropertySet.hbm.xml" + }; + } +} diff --git a/hibernate-core/src/test/resources/org/hibernate/test/any/AnyTestEagerPropertySet.hbm.xml b/hibernate-core/src/test/resources/org/hibernate/test/any/AnyTestEagerPropertySet.hbm.xml new file mode 100644 index 0000000000..19261fecbc --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/test/any/AnyTestEagerPropertySet.hbm.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hibernate-core/src/test/resources/org/hibernate/test/any/AnyTestLazyPropertySet.hbm.xml b/hibernate-core/src/test/resources/org/hibernate/test/any/AnyTestLazyPropertySet.hbm.xml new file mode 100644 index 0000000000..210015f601 --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/test/any/AnyTestLazyPropertySet.hbm.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file