Skip to content

Commit dadfa62

Browse files
committed
HHH-17131 Add test for issue
1 parent 74cc07d commit dadfa62

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
6+
*/
7+
package org.hibernate.orm.test.stream;
8+
9+
import java.util.HashSet;
10+
import java.util.List;
11+
import java.util.Set;
12+
import java.util.stream.Collectors;
13+
import java.util.stream.Stream;
14+
15+
import org.hibernate.dialect.DB2Dialect;
16+
import org.hibernate.dialect.DerbyDialect;
17+
18+
import org.hibernate.testing.orm.junit.DomainModel;
19+
import org.hibernate.testing.orm.junit.Jira;
20+
import org.hibernate.testing.orm.junit.SessionFactory;
21+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
22+
import org.hibernate.testing.orm.junit.SkipForDialect;
23+
import org.junit.jupiter.api.AfterAll;
24+
import org.junit.jupiter.api.BeforeAll;
25+
import org.junit.jupiter.api.Test;
26+
27+
import jakarta.persistence.CascadeType;
28+
import jakarta.persistence.Entity;
29+
import jakarta.persistence.GeneratedValue;
30+
import jakarta.persistence.Id;
31+
import jakarta.persistence.JoinColumn;
32+
import jakarta.persistence.ManyToOne;
33+
import jakarta.persistence.MappedSuperclass;
34+
import jakarta.persistence.OneToMany;
35+
import jakarta.persistence.Tuple;
36+
37+
import static org.assertj.core.api.Assertions.assertThat;
38+
39+
/**
40+
* @author Marco Belladelli
41+
*/
42+
@DomainModel( annotatedClasses = {
43+
NestedCollectionFetchStreamTest.BasicEntity.class,
44+
NestedCollectionFetchStreamTest.EntityA.class,
45+
NestedCollectionFetchStreamTest.EntityB.class,
46+
NestedCollectionFetchStreamTest.EntityC.class,
47+
} )
48+
@SessionFactory
49+
@Jira( "https://hibernate.atlassian.net/browse/HHH-17131" )
50+
@SkipForDialect( dialectClass = DB2Dialect.class, reason = "The support for ResultSet#isBeforeFirst() is optional for ResultSets with a result set type of TYPE_FORWARD_ONLY and DB2 does not support it" )
51+
@SkipForDialect( dialectClass = DerbyDialect.class, reason = "The support for ResultSet#isBeforeFirst() is optional for ResultSets with a result set type of TYPE_FORWARD_ONLY and Derby does not support it" )
52+
public class NestedCollectionFetchStreamTest {
53+
@BeforeAll
54+
public void setUp(SessionFactoryScope scope) {
55+
scope.inTransaction( session -> {
56+
final EntityB b = new EntityB();
57+
b.getC().addAll( Stream.generate( EntityC::new ).limit( 3 ).collect( Collectors.toSet() ) );
58+
session.persist( b );
59+
session.persist( new EntityA( b ) );
60+
} );
61+
}
62+
63+
@AfterAll
64+
public void tearDown(SessionFactoryScope scope) {
65+
scope.inTransaction( session -> {
66+
session.createMutationQuery( "delete from EntityA" ).executeUpdate();
67+
session.createMutationQuery( "delete from EntityC" ).executeUpdate();
68+
session.createMutationQuery( "delete from EntityB" ).executeUpdate();
69+
} );
70+
}
71+
72+
@Test
73+
public void testImplicitNestedCollection(SessionFactoryScope scope) {
74+
scope.inTransaction( session -> {
75+
try (final Stream<EntityA> stream = session.createQuery(
76+
"select a from EntityA a left join fetch a.b b left join fetch b.c c",
77+
EntityA.class
78+
).getResultStream()) {
79+
final List<EntityA> list = stream.collect( Collectors.toList() );
80+
assertThat( list ).hasSize( 1 );
81+
assertThat( list.get( 0 ).getB() ).isNotNull();
82+
assertThat( list.get( 0 ).getB().getC() ).hasSize( 3 );
83+
}
84+
} );
85+
}
86+
87+
@Test
88+
public void testExplicitNestedCollection(SessionFactoryScope scope) {
89+
scope.inTransaction( session -> {
90+
try (final Stream<Tuple> stream = session.createQuery(
91+
"select a, b from EntityA a left join fetch a.b b left join fetch b.c c",
92+
Tuple.class
93+
).getResultStream()) {
94+
final List<Tuple> list = stream.collect( Collectors.toList() );
95+
assertThat( list ).hasSize( 1 );
96+
assertThat( list.get( 0 ).get( 1, EntityB.class ) ).isNotNull();
97+
assertThat( list.get( 0 ).get( 1, EntityB.class ).getC() ).hasSize( 3 );
98+
}
99+
} );
100+
}
101+
102+
@MappedSuperclass
103+
public static abstract class BasicEntity {
104+
@Id
105+
@GeneratedValue
106+
private Long id;
107+
}
108+
109+
@Entity( name = "EntityA" )
110+
public static class EntityA extends BasicEntity {
111+
@ManyToOne
112+
private EntityB b;
113+
114+
public EntityA() {
115+
}
116+
117+
public EntityA(EntityB b) {
118+
this.b = b;
119+
}
120+
121+
public EntityB getB() {
122+
return b;
123+
}
124+
}
125+
126+
@Entity( name = "EntityB" )
127+
public static class EntityB extends BasicEntity {
128+
@OneToMany( cascade = CascadeType.PERSIST )
129+
@JoinColumn( name = "entityb_id" )
130+
private Set<EntityC> c = new HashSet<>();
131+
132+
public Set<EntityC> getC() {
133+
return c;
134+
}
135+
}
136+
137+
@Entity( name = "EntityC" )
138+
public static class EntityC extends BasicEntity {
139+
}
140+
}

0 commit comments

Comments
 (0)