Skip to content

Commit b760ce1

Browse files
committed
HHH-19874 Add additional test
1 parent 23c3d08 commit b760ce1

File tree

1 file changed

+228
-0
lines changed

1 file changed

+228
-0
lines changed
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.query;
6+
7+
import jakarta.persistence.ElementCollection;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.GeneratedValue;
10+
import jakarta.persistence.Id;
11+
import jakarta.persistence.ManyToMany;
12+
import jakarta.persistence.ManyToOne;
13+
import jakarta.persistence.Table;
14+
import jakarta.persistence.Transient;
15+
import jakarta.persistence.Version;
16+
import org.hibernate.annotations.Fetch;
17+
import org.hibernate.annotations.FetchMode;
18+
import org.hibernate.testing.orm.junit.DomainModel;
19+
import org.hibernate.testing.orm.junit.JiraKey;
20+
import org.hibernate.testing.orm.junit.SessionFactory;
21+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
22+
import org.junit.jupiter.api.AfterEach;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
26+
import java.util.ArrayList;
27+
import java.util.Arrays;
28+
import java.util.HashSet;
29+
import java.util.List;
30+
import java.util.Objects;
31+
import java.util.Set;
32+
33+
import static jakarta.persistence.CascadeType.PERSIST;
34+
import static jakarta.persistence.FetchType.EAGER;
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
import static org.hibernate.annotations.FetchMode.JOIN;
37+
import static org.hibernate.annotations.FetchMode.SUBSELECT;
38+
39+
@DomainModel(
40+
annotatedClasses = {
41+
SubselectFetch2Test.NodeHolder.class,
42+
SubselectFetch2Test.NodeIntermediateHolder.class,
43+
SubselectFetch2Test.Element.class,
44+
SubselectFetch2Test.Node.class,
45+
}
46+
)
47+
@SessionFactory
48+
public class SubselectFetch2Test {
49+
50+
@BeforeEach
51+
public void setUp(SessionFactoryScope scope) {
52+
scope.inTransaction(
53+
session -> {
54+
Node basik = new Node( "Child" );
55+
Element n1, n2;
56+
basik.elements.add( n1 = new Element( basik ) );
57+
basik.elements.add( n2 = new Element( basik ) );
58+
basik.elements.add( new Element( basik ) );
59+
60+
Node node2 = new Node( "Child2" );
61+
node2.parent = new NodeIntermediateHolder( basik );
62+
node2.parent.strings = new ArrayList<>( Arrays.asList("s1", "s2"));
63+
node2.elements.add( n1 );
64+
node2.elements.add( n2 );
65+
node2.elements.add( new Element( basik ) );
66+
67+
NodeHolder root = new NodeHolder( basik, node2 );
68+
session.persist( root );
69+
70+
session.persist( new NodeHolder( null, null ) );
71+
}
72+
);
73+
}
74+
75+
@AfterEach
76+
@JiraKey("HHH-19868")
77+
public void tearDown(SessionFactoryScope scope) {
78+
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
79+
}
80+
81+
@Test
82+
void test2(SessionFactoryScope scope) {
83+
scope.inTransaction( session -> {
84+
NodeHolder holder = session.createSelectionQuery( "from NodeHolder nh join fetch nh.node1 join fetch nh.node2", NodeHolder.class ).getSingleResult();
85+
assertThat( holder.node1.elements ).hasSize( 3 );
86+
assertThat( holder.node2.elements ).hasSize( 3 );
87+
} );
88+
}
89+
90+
@Test
91+
void test3(SessionFactoryScope scope) {
92+
scope.inTransaction( session -> {
93+
session.createSelectionQuery( "select nh, n1, n2 from NodeHolder nh join nh.node1 n1 join nh.node2 n2" ).getResultList();
94+
} );
95+
}
96+
97+
@Entity(name = "NodeHolder")
98+
public static class NodeHolder {
99+
@Id
100+
@GeneratedValue
101+
Integer id;
102+
103+
@ManyToOne(cascade = PERSIST)
104+
Node node1;
105+
@ManyToOne(cascade = PERSIST)
106+
Node node2;
107+
108+
public NodeHolder(Node node1, Node node2) {
109+
this.node1 = node1;
110+
this.node2 = node2;
111+
}
112+
113+
public NodeHolder() {
114+
}
115+
}
116+
117+
118+
@Entity(name = "NodeIntermediateHolder")
119+
public static class NodeIntermediateHolder {
120+
@Id
121+
@GeneratedValue
122+
Integer id;
123+
124+
@ManyToOne(cascade = PERSIST)
125+
Node node;
126+
127+
@ElementCollection(fetch = EAGER)
128+
@Fetch( JOIN )
129+
List<String> strings;
130+
131+
public NodeIntermediateHolder(Node node) {
132+
this.node = node;
133+
}
134+
135+
public NodeIntermediateHolder() {
136+
}
137+
}
138+
139+
@Entity(name = "Element")
140+
@Table(name = "Element")
141+
public static class Element {
142+
@Id
143+
@GeneratedValue
144+
Integer id;
145+
146+
@ManyToOne
147+
@Fetch(FetchMode.SELECT)
148+
Node node;
149+
150+
public Element(Node node) {
151+
this.node = node;
152+
}
153+
154+
public Element() {
155+
}
156+
}
157+
158+
@Entity(name = "Node")
159+
@Table(name = "Node")
160+
public static class Node {
161+
162+
@Id
163+
@GeneratedValue
164+
Integer id;
165+
@Version
166+
Integer version;
167+
String string;
168+
@Transient
169+
boolean loaded = false;
170+
171+
@ManyToOne(fetch = EAGER, cascade = PERSIST)
172+
NodeIntermediateHolder parent;
173+
174+
@ManyToMany(fetch = EAGER, cascade = PERSIST)
175+
@Fetch(SUBSELECT)
176+
Set<Element> elements = new HashSet<>();
177+
178+
public Node(String string) {
179+
this.string = string;
180+
}
181+
182+
public Node() {
183+
}
184+
185+
public Integer getId() {
186+
return id;
187+
}
188+
189+
public void setId(Integer id) {
190+
this.id = id;
191+
}
192+
193+
public String getString() {
194+
return string;
195+
}
196+
197+
public void setString(String string) {
198+
this.string = string;
199+
}
200+
201+
// @PostLoad
202+
// void postLoad() {
203+
// loaded = true;
204+
// }
205+
206+
@Override
207+
public String toString() {
208+
return id + ": " + string;
209+
}
210+
211+
@Override
212+
public boolean equals(Object o) {
213+
if ( this == o ) {
214+
return true;
215+
}
216+
if ( o == null || getClass() != o.getClass() ) {
217+
return false;
218+
}
219+
Node node = (Node) o;
220+
return Objects.equals( string, node.string );
221+
}
222+
223+
@Override
224+
public int hashCode() {
225+
return Objects.hash( string );
226+
}
227+
}
228+
}

0 commit comments

Comments
 (0)