diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/PluralAttributeElementSourceManyToAnyImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/PluralAttributeElementSourceManyToAnyImpl.java
index d5e33a0b32..65f145e09d 100644
--- a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/PluralAttributeElementSourceManyToAnyImpl.java
+++ b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/PluralAttributeElementSourceManyToAnyImpl.java
@@ -32,6 +32,7 @@ public class PluralAttributeElementSourceManyToAnyImpl
private final AnyDiscriminatorSource discriminatorSource;
private final AnyKeySource keySource;
+ private final boolean isLazy;
public PluralAttributeElementSourceManyToAnyImpl(
final MappingDocument mappingDocument,
@@ -139,6 +140,8 @@ public class PluralAttributeElementSourceManyToAnyImpl
return mappingDocument;
}
};
+
+ this.isLazy = jaxbManyToAnyMapping.isLazy();
}
@Override
@@ -155,4 +158,9 @@ public class PluralAttributeElementSourceManyToAnyImpl
public PluralAttributeElementNature getNature() {
return PluralAttributeElementNature.MANY_TO_ANY;
}
+
+ @Override
+ public boolean isLazy() {
+ return isLazy;
+ }
}
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 4480d040dc..0d1a4006b4 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
@@ -720,7 +720,7 @@ public abstract class CollectionBinder {
fetchType = elementCollection.fetch();
}
else if ( manyToAny != null ) {
- fetchType = FetchType.LAZY;
+ fetchType = manyToAny.fetch();
}
else {
throw new AssertionFailure(
diff --git a/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-3.0.dtd b/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-3.0.dtd
index 9de0ef239a..9e658b9f6e 100644
--- a/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-3.0.dtd
+++ b/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-3.0.dtd
@@ -891,6 +891,7 @@ holding the name of the class (for that row). -->
+
diff --git a/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/legacy-mapping-4.0.xsd b/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/legacy-mapping-4.0.xsd
index fea059f7f0..1597fbce9f 100644
--- a/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/legacy-mapping-4.0.xsd
+++ b/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/legacy-mapping-4.0.xsd
@@ -1485,6 +1485,7 @@
+
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 0a8f6bbcec..47d7eafbb3 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
@@ -211,6 +211,57 @@ public class AnyTest extends BaseCoreFunctionalTestCase {
}
}
+ @Test
+ public void testManyToAnyFetchEager() {
+ doInHibernate( this::sessionFactory, s -> {
+ PropertySet set = new PropertySet( "string" );
+ Property property = new StringProperty( "name", "Alex" );
+ set.addGeneralProperty( 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.getGeneralProperties() );
+ assertEquals( 1, result.getGeneralProperties().size() );
+ assertEquals( "Alex", result.getGeneralProperties().get( 0 ).asString() );
+ }
+
+ @Test
+ public void testManyToAnyFetchLazy() {
+ doInHibernate( this::sessionFactory, s -> {
+ LazyPropertySet set = new LazyPropertySet( "string" );
+ Property property = new StringProperty( "name", "Alex" );
+ set.addGeneralProperty( 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.getGeneralProperties() );
+
+ try {
+ result.getGeneralProperties().get( 0 );
+ 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[] {
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
index f29b5a6552..9088dd2cb6 100644
--- 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
@@ -6,18 +6,22 @@
*/
package org.hibernate.test.annotations.any;
+import java.util.ArrayList;
+import java.util.List;
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.JoinTable;
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.ManyToAny;
import org.hibernate.annotations.MetaValue;
@Entity
@@ -26,6 +30,7 @@ public class LazyPropertySet {
private Integer id;
private String name;
private Property someProperty;
+ private List generalProperties = new ArrayList();
public LazyPropertySet() {
super();
@@ -67,4 +72,26 @@ public class LazyPropertySet {
public void setSomeProperty(Property someProperty) {
this.someProperty = someProperty;
}
+
+ @ManyToAny(
+ metaColumn = @Column( name = "property_type" ),
+ fetch = FetchType.LAZY )
+ @AnyMetaDef( idType = "integer", metaType = "string",
+ metaValues = {
+ @MetaValue( value = "S", targetEntity = StringProperty.class ),
+ @MetaValue( value = "I", targetEntity = IntegerProperty.class ) } )
+ @Cascade( { org.hibernate.annotations.CascadeType.ALL } )
+ @JoinTable( name = "lazy_obj_properties", joinColumns = @JoinColumn( name = "obj_id" ),
+ inverseJoinColumns = @JoinColumn( name = "property_id" ) )
+ public List getGeneralProperties() {
+ return generalProperties;
+ }
+
+ public void setGeneralProperties(List generalProperties) {
+ this.generalProperties = generalProperties;
+ }
+
+ 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
index 91626534b7..85eb5c0f65 100644
--- a/hibernate-core/src/test/java/org/hibernate/test/any/AnyEagerHbmTest.java
+++ b/hibernate-core/src/test/java/org/hibernate/test/any/AnyEagerHbmTest.java
@@ -43,6 +43,27 @@ public class AnyEagerHbmTest extends BaseCoreFunctionalTestCase {
assertEquals( "Alex", result.getSomeProperty().asString() );
}
+ @Test
+ public void testManyToAnyFetchEager() {
+ 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.addGeneralProperty( 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.getGeneralProperties() );
+ assertEquals( 1, result.getGeneralProperties().size() );
+ assertEquals( "Alex", result.getGeneralProperties().get( 0 ).asString() );
+ }
+
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
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
index 1a5457c57a..09ceef268d 100644
--- a/hibernate-core/src/test/java/org/hibernate/test/any/AnyLazyHbmTest.java
+++ b/hibernate-core/src/test/java/org/hibernate/test/any/AnyLazyHbmTest.java
@@ -56,6 +56,36 @@ public class AnyLazyHbmTest extends BaseCoreFunctionalTestCase {
}
}
+ @Test
+ public void testManyToAnyFetchLazy() {
+ doInHibernate( this::sessionFactory, s -> {
+ org.hibernate.test.annotations.any.LazyPropertySet set = new org.hibernate.test.annotations.any.LazyPropertySet( "string" );
+ Property property = new StringProperty( "name", "Alex" );
+ set.addGeneralProperty( property );
+ s.save( set );
+ } );
+
+ org.hibernate.test.annotations.any.LazyPropertySet result = doInHibernate( this::sessionFactory, s -> {
+ return s.createQuery( "select s from LazyPropertySet s where name = :name", org.hibernate.test.annotations.any.LazyPropertySet.class )
+ .setParameter( "name", "string" )
+ .getSingleResult();
+ } );
+
+ assertNotNull( result );
+ assertNotNull( result.getGeneralProperties() );
+
+ try {
+ result.getGeneralProperties().get( 0 );
+ 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[] {
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
index 19261fecbc..28b87c0f5f 100644
--- 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
@@ -10,7 +10,7 @@
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
-
+
@@ -23,5 +23,17 @@
+
+
+
+
+
+
+
+
+
+
+
+
-
\ 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
index 210015f601..5842707d8c 100644
--- 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
@@ -23,5 +23,17 @@
+
+
+
+
+
+
+
+
+
+
+
+
-
\ No newline at end of file
+