Skip to content

Commit 2bae1f5

Browse files
committed
HHH-19607 Add test for issue
1 parent 50177c5 commit 2bae1f5

File tree

1 file changed

+206
-0
lines changed

1 file changed

+206
-0
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.inheritance.embeddable;
6+
7+
import jakarta.persistence.CascadeType;
8+
import jakarta.persistence.Embeddable;
9+
import jakarta.persistence.Embedded;
10+
import jakarta.persistence.Entity;
11+
import jakarta.persistence.GeneratedValue;
12+
import jakarta.persistence.Id;
13+
import jakarta.persistence.Inheritance;
14+
import jakarta.persistence.InheritanceType;
15+
import jakarta.persistence.ManyToMany;
16+
import org.hibernate.testing.orm.junit.DomainModel;
17+
import org.hibernate.testing.orm.junit.JiraKey;
18+
import org.hibernate.testing.orm.junit.SessionFactory;
19+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
20+
import org.junit.jupiter.api.AfterEach;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.util.HashSet;
25+
import java.util.List;
26+
import java.util.Objects;
27+
import java.util.Set;
28+
29+
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
30+
31+
@DomainModel(
32+
annotatedClasses = {
33+
JoinInheritanceSelectJoinTest.Human.class,
34+
JoinInheritanceSelectJoinTest.Parent.class,
35+
JoinInheritanceSelectJoinTest.Child.class,
36+
}
37+
)
38+
@SessionFactory
39+
@JiraKey("HHH-19607")
40+
class JoinInheritanceSelectJoinTest {
41+
42+
@BeforeEach
43+
public void setup(SessionFactoryScope scope) {
44+
scope.inTransaction(
45+
session -> {
46+
Parent parent = new Parent( "Luigi", new Address( "Via Milano", "Roma" ) );
47+
Child child = new Child( "Miriam" );
48+
child.addParent( parent );
49+
session.persist( child );
50+
}
51+
);
52+
}
53+
54+
@AfterEach
55+
public void teardown(SessionFactoryScope scope) {
56+
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
57+
}
58+
59+
@Test
60+
void testSelect(SessionFactoryScope scope) {
61+
scope.inTransaction( session -> {
62+
List<Child> children = session
63+
.createSelectionQuery(
64+
"SELECT c FROM Child c LEFT JOIN c.parents p WHERE p.address is not null",
65+
Child.class
66+
)
67+
.getResultList();
68+
69+
assertThat( children.size() ).isEqualTo( 1 );
70+
} );
71+
}
72+
73+
@Test
74+
void testSelectWithParameters(SessionFactoryScope scope) {
75+
scope.inTransaction( session -> {
76+
List<Child> children = session
77+
.createSelectionQuery(
78+
"SELECT c FROM Child c LEFT JOIN c.parents p WHERE p.address = :address",
79+
Child.class
80+
)
81+
.setParameter( "address", new Address( "Via Milano", "Roma" ) )
82+
.getResultList();
83+
84+
assertThat( children.size() ).isEqualTo( 1 );
85+
} );
86+
}
87+
88+
89+
90+
@Entity(name = "Child")
91+
public static class Child {
92+
93+
@Id
94+
@GeneratedValue
95+
private Long id;
96+
97+
private String name;
98+
99+
@ManyToMany(cascade = CascadeType.PERSIST)
100+
private Set<Parent> parents = new HashSet<>();
101+
102+
public Child() {
103+
}
104+
105+
public Child(String name) {
106+
this.name = name;
107+
}
108+
109+
public Long getId() {
110+
return id;
111+
}
112+
113+
public String getName() {
114+
return name;
115+
}
116+
117+
public Set<Parent> getParents() {
118+
return parents;
119+
}
120+
121+
public void addParent(Parent parent) {
122+
this.parents.add( parent );
123+
}
124+
}
125+
126+
@Entity(name = "Human")
127+
@Inheritance(strategy = InheritanceType.JOINED)
128+
public static class Human {
129+
@Id
130+
@GeneratedValue
131+
private Long id;
132+
133+
private String name;
134+
135+
@Embedded
136+
private Address address;
137+
138+
public Human() {
139+
}
140+
141+
public Human(String name, Address address) {
142+
this.name = name;
143+
this.address = address;
144+
}
145+
146+
public Long getId() {
147+
return id;
148+
}
149+
150+
public String getName() {
151+
return name;
152+
}
153+
154+
public Address getAddress() {
155+
return address;
156+
}
157+
}
158+
159+
@Entity(name = "Parent")
160+
public static class Parent extends Human {
161+
public Parent() {
162+
}
163+
164+
public Parent(String name, Address address) {
165+
super( name, address );
166+
}
167+
}
168+
169+
@Embeddable
170+
public static class Address {
171+
private String street;
172+
173+
private String city;
174+
175+
public Address() {
176+
}
177+
178+
public Address(String street, String city) {
179+
this.street = street;
180+
this.city = city;
181+
}
182+
183+
public String getStreet() {
184+
return street;
185+
}
186+
187+
public String getCity() {
188+
return city;
189+
}
190+
191+
@Override
192+
public boolean equals(Object o) {
193+
if ( o == null || getClass() != o.getClass() ) {
194+
return false;
195+
}
196+
Address address = (Address) o;
197+
return Objects.equals( street, address.street ) && Objects.equals( city, address.city );
198+
}
199+
200+
@Override
201+
public int hashCode() {
202+
return Objects.hash( street, city );
203+
}
204+
}
205+
206+
}

0 commit comments

Comments
 (0)