Skip to content

Commit 95779d4

Browse files
committed
HHH-19607 Add test for issue
1 parent c4fd9ba commit 95779d4

File tree

1 file changed

+207
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)