HHH-13243 - Respect @ManyToAny.fetch setting to FetchType.EAGER

This commit is contained in:
John Lin 2019-02-01 15:10:56 +08:00
parent d158762144
commit 4b37e02382
10 changed files with 167 additions and 4 deletions

View File

@ -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;
}
}

View File

@ -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(

View File

@ -891,6 +891,7 @@ holding the name of the class (for that row). -->
<!ELEMENT many-to-any (meta-value*,column, column+)>
<!ATTLIST many-to-any id-type CDATA #REQUIRED>
<!ATTLIST many-to-any meta-type CDATA #IMPLIED> <!--- default: Hibernate.CLASS -->
<!ATTLIST any lazy (true|false) "false">
<!ELEMENT index-many-to-any (column, column+)>
<!ATTLIST index-many-to-any id-type CDATA #REQUIRED>

View File

@ -1485,6 +1485,7 @@
<xs:attribute name="id-type" use="required" type="xs:string"/>
<xs:attribute name="meta-type" type="xs:string"/>
<!--- default: Hibernate.CLASS -->
<xs:attribute name="lazy" default="false" type="xs:boolean"/>
</xs:complexType>

View File

@ -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[] {

View File

@ -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<Property> generalProperties = new ArrayList<Property>();
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<Property> getGeneralProperties() {
return generalProperties;
}
public void setGeneralProperties(List<Property> generalProperties) {
this.generalProperties = generalProperties;
}
public void addGeneralProperty(Property property) {
this.generalProperties.add( property );
}
}

View File

@ -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[] {

View File

@ -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[] {

View File

@ -10,7 +10,7 @@
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.annotations.any">
<class name="PropertySet" table="lazy_property_set">
<class name="PropertySet" table="property_set">
<id name="id" type="java.lang.Integer">
<generator class="increment"/>
</id>
@ -23,5 +23,17 @@
<column name="PROP_TYPE"/>
<column name="property_id"/>
</any>
<list name="generalProperties" table="obj_properties" cascade="all" lazy="false">
<key column="obj_id"/>
<list-index column = "idx"/>
<many-to-any id-type="integer" meta-type="string">
<meta-value value="S" class="StringProperty"/>
<meta-value value="I" class="IntegerProperty"/>
<column name="PROP_TYPE"/>
<column name="property_id"/>
</many-to-any>
</list>
</class>
</hibernate-mapping>
</hibernate-mapping>

View File

@ -23,5 +23,17 @@
<column name="PROP_TYPE"/>
<column name="property_id"/>
</any>
<list name="generalProperties" table="lazy_obj_properties" cascade="all" lazy="true">
<key column="obj_id"/>
<list-index column = "idx"/>
<many-to-any id-type="integer" meta-type="string">
<meta-value value="S" class="StringProperty"/>
<meta-value value="I" class="IntegerProperty"/>
<column name="PROP_TYPE"/>
<column name="property_id"/>
</many-to-any>
</list>
</class>
</hibernate-mapping>
</hibernate-mapping>