Skip to content

Commit f899246

Browse files
johnlinpdreab8
authored andcommitted
HHH-13243 Respect @ManyToAny.fetch setting to FetchType.EAGER
1 parent 5b62264 commit f899246

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,7 @@ private FetchType getJpaFetchType() {
15291529
}
15301530

15311531
if ( manyToAny != null ) {
1532-
return LAZY;
1532+
return manyToAny.fetch();
15331533
}
15341534

15351535
throw new AssertionFailure(
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.any.annotations;
6+
7+
import org.hibernate.LazyInitializationException;
8+
import org.hibernate.testing.orm.junit.DomainModel;
9+
import org.hibernate.testing.orm.junit.JiraKey;
10+
import org.hibernate.testing.orm.junit.SessionFactory;
11+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
12+
import org.junit.jupiter.api.AfterEach;
13+
import org.junit.jupiter.api.Assertions;
14+
import org.junit.jupiter.api.Test;
15+
16+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
17+
18+
@DomainModel(
19+
annotatedPackageNames = "org.hibernate.orm.test.any.annotations",
20+
annotatedClasses = {
21+
StringProperty.class,
22+
IntegerProperty.class,
23+
PropertySet.class,
24+
LazyPropertySet.class,
25+
}
26+
)
27+
@SessionFactory
28+
@JiraKey( "HHH-13243" )
29+
public class AnyFetchEagerTest {
30+
@AfterEach
31+
public void dropTestData(SessionFactoryScope scope) {
32+
scope.inTransaction(
33+
session -> {
34+
session.createMutationQuery( "delete StringProperty" ).executeUpdate();
35+
session.createMutationQuery( "delete IntegerProperty" ).executeUpdate();
36+
session.createMutationQuery( "delete PropertySet" ).executeUpdate();
37+
session.createMutationQuery( "delete LazyPropertySet" ).executeUpdate();
38+
}
39+
);
40+
}
41+
42+
43+
@Test
44+
public void testManyToAnyFetchEager(SessionFactoryScope scope) {
45+
scope.inTransaction( s -> {
46+
PropertySet set = new PropertySet( "string" );
47+
Property property = new StringProperty( "name", "Alex" );
48+
set.addGeneralProperty( property );
49+
s.persist( set );
50+
} );
51+
52+
PropertySet result = scope.fromTransaction(
53+
s -> s.createQuery( "select s from PropertySet s where name = :name", PropertySet.class )
54+
.setParameter( "name", "string" )
55+
.getSingleResult() );
56+
57+
assertThat( result ).isNotNull();
58+
assertThat( result.getGeneralProperties() ).isNotNull();
59+
assertThat( result.getGeneralProperties().size() ).isEqualTo( 1 );
60+
assertThat( result.getGeneralProperties().get(0).asString() ).isEqualTo( "Alex" );
61+
}
62+
63+
@Test
64+
public void testManyToAnyFetchLazy(SessionFactoryScope scope) {
65+
scope.inTransaction( s -> {
66+
LazyPropertySet set = new LazyPropertySet( "string" );
67+
Property property = new StringProperty( "name", "Alex" );
68+
set.addGeneralProperty( property );
69+
s.persist( set );
70+
} );
71+
72+
LazyPropertySet result = scope.fromTransaction(
73+
s -> s.createQuery( "select s from LazyPropertySet s where name = :name", LazyPropertySet.class )
74+
.setParameter( "name", "string" )
75+
.getSingleResult() );
76+
77+
assertThat( result ).isNotNull();
78+
assertThat( result.getGeneralProperties() ).isNotNull();
79+
80+
try {
81+
result.getGeneralProperties().get(0);
82+
Assertions.fail( "should not get the property string after session closed." );
83+
}
84+
catch (LazyInitializationException e) {
85+
// expected
86+
}
87+
catch (Exception e) {
88+
Assertions.fail( "should not throw exception other than LazyInitializationException." );
89+
}
90+
}
91+
}

hibernate-core/src/test/java/org/hibernate/orm/test/any/annotations/LazyPropertySet.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.orm.test.any.annotations;
66

7+
import jakarta.persistence.JoinTable;
78
import org.hibernate.annotations.Any;
89
import org.hibernate.annotations.AnyDiscriminator;
910
import org.hibernate.annotations.AnyDiscriminatorValue;
@@ -19,13 +20,18 @@
1920
import jakarta.persistence.Id;
2021
import jakarta.persistence.JoinColumn;
2122
import jakarta.persistence.Table;
23+
import org.hibernate.annotations.ManyToAny;
24+
25+
import java.util.ArrayList;
26+
import java.util.List;
2227

2328
@Entity
2429
@Table( name = "lazy_property_set" )
2530
public class LazyPropertySet {
2631
private Integer id;
2732
private String name;
2833
private Property someProperty;
34+
private List<Property> generalProperties = new ArrayList<Property>();
2935

3036
public LazyPropertySet() {
3137
super();
@@ -68,4 +74,26 @@ public Property getSomeProperty() {
6874
public void setSomeProperty(Property someProperty) {
6975
this.someProperty = someProperty;
7076
}
77+
78+
@ManyToAny(
79+
fetch = FetchType.LAZY )
80+
@Column( name = "property_type" )
81+
@AnyDiscriminator( DiscriminatorType.STRING )
82+
@AnyKeyJavaClass( Integer.class )
83+
@AnyDiscriminatorValue( discriminator = "S", entity = StringProperty.class )
84+
@AnyDiscriminatorValue( discriminator = "I", entity = IntegerProperty.class )
85+
@Cascade( { org.hibernate.annotations.CascadeType.ALL } )
86+
@JoinTable( name = "lazy_obj_properties", joinColumns = @JoinColumn( name = "obj_id" ),
87+
inverseJoinColumns = @JoinColumn( name = "property_id" ) )
88+
public List<Property> getGeneralProperties() {
89+
return generalProperties;
90+
}
91+
92+
public void setGeneralProperties(List<Property> generalProperties) {
93+
this.generalProperties = generalProperties;
94+
}
95+
96+
public void addGeneralProperty(Property property) {
97+
this.generalProperties.add( property );
98+
}
7199
}

0 commit comments

Comments
 (0)