Skip to content

Commit 2349081

Browse files
mbelladebeikov
authored andcommitted
HHH-17405 Add test for issue
1 parent d829329 commit 2349081

File tree

1 file changed

+211
-0
lines changed

1 file changed

+211
-0
lines changed
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
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.annotations.generics;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
import org.hibernate.query.sqm.tree.domain.SqmPath;
13+
14+
import org.hibernate.testing.orm.junit.DomainModel;
15+
import org.hibernate.testing.orm.junit.Jira;
16+
import org.hibernate.testing.orm.junit.SessionFactory;
17+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
18+
import org.junit.jupiter.api.AfterAll;
19+
import org.junit.jupiter.api.BeforeAll;
20+
import org.junit.jupiter.api.Test;
21+
22+
import jakarta.persistence.Entity;
23+
import jakarta.persistence.GeneratedValue;
24+
import jakarta.persistence.Id;
25+
import jakarta.persistence.ManyToOne;
26+
import jakarta.persistence.MappedSuperclass;
27+
import jakarta.persistence.OneToMany;
28+
import jakarta.persistence.criteria.CriteriaBuilder;
29+
import jakarta.persistence.criteria.CriteriaQuery;
30+
import jakarta.persistence.criteria.Path;
31+
import jakarta.persistence.criteria.Root;
32+
33+
import static org.assertj.core.api.Assertions.assertThat;
34+
35+
/**
36+
* @author Marco Belladelli
37+
*/
38+
@DomainModel( annotatedClasses = {
39+
GenericMappedSuperclassAssociationTest.Parent.class,
40+
GenericMappedSuperclassAssociationTest.ParentA.class,
41+
GenericMappedSuperclassAssociationTest.ParentB.class,
42+
GenericMappedSuperclassAssociationTest.Child.class,
43+
GenericMappedSuperclassAssociationTest.ChildA.class,
44+
GenericMappedSuperclassAssociationTest.ChildB.class,
45+
} )
46+
@SessionFactory
47+
@Jira( "https://hibernate.atlassian.net/browse/HHH-17405" )
48+
public class GenericMappedSuperclassAssociationTest {
49+
@BeforeAll
50+
public void setUp(SessionFactoryScope scope) {
51+
scope.inTransaction( session -> {
52+
final ParentA parentA = new ParentA( 1L, "parent_a" );
53+
session.persist( parentA );
54+
session.persist( new ChildA( "child_a_1", parentA ) );
55+
session.persist( new ChildA( "child_a_2", parentA ) );
56+
final ParentB parentB = new ParentB( 2L, "parent_b" );
57+
session.persist( parentB );
58+
session.persist( new ChildB( "child_b", parentB ) );
59+
} );
60+
}
61+
62+
@AfterAll
63+
public void tearDown(SessionFactoryScope scope) {
64+
scope.inTransaction( session -> {
65+
session.createMutationQuery( String.format( "delete from %s", Child.class.getName() ) ).executeUpdate();
66+
session.createMutationQuery( String.format( "delete from %s", Parent.class.getName() ) ).executeUpdate();
67+
} );
68+
}
69+
70+
@Test
71+
public void testHqlQuery(SessionFactoryScope scope) {
72+
scope.inTransaction( session -> {
73+
final List<ChildA> resultList = session.createQuery(
74+
"select c from ChildA c where c.parent.id = 1",
75+
ChildA.class
76+
).getResultList();
77+
assertThat( resultList ).hasSize( 2 );
78+
resultList.forEach( c -> assertThat( c.getParent().getName() ).isEqualTo( "parent_a" ) );
79+
} );
80+
}
81+
82+
@Test
83+
public void testCriteriaQuery(SessionFactoryScope scope) {
84+
scope.inTransaction( session -> {
85+
final CriteriaBuilder cb = session.getCriteriaBuilder();
86+
final CriteriaQuery<ChildB> cq = cb.createQuery( ChildB.class );
87+
final Root<ChildB> from = cq.from( ChildB.class );
88+
final Path<Object> parent = from.get( "parent" );
89+
assertThat( parent.getModel().getBindableJavaType() ).isEqualTo( Parent.class );
90+
assertThat( ( (SqmPath<?>) parent ).getResolvedModel().getBindableJavaType() ).isEqualTo( ParentB.class );
91+
cq.select( from ).where( cb.equal( from.get( "parent" ).get( "id" ), 2L ) );
92+
final ChildB result = session.createQuery( cq ).getSingleResult();
93+
assertThat( result.getParent().getName() ).isEqualTo( "parent_b" );
94+
} );
95+
}
96+
97+
@MappedSuperclass
98+
public static abstract class Child<P extends Parent<?>> {
99+
@Id
100+
@GeneratedValue
101+
private Long id;
102+
103+
private String name;
104+
105+
@ManyToOne
106+
private P parent;
107+
108+
public Child() {
109+
}
110+
111+
public Child(String name, P parent) {
112+
this.name = name;
113+
this.parent = parent;
114+
}
115+
116+
public Long getId() {
117+
return id;
118+
}
119+
120+
public String getName() {
121+
return name;
122+
}
123+
124+
public P getParent() {
125+
return parent;
126+
}
127+
}
128+
129+
@MappedSuperclass
130+
public static abstract class Parent<C extends Child<?>> {
131+
private String name;
132+
133+
@OneToMany( mappedBy = "parent" )
134+
private List<C> children = new ArrayList<>();
135+
136+
public Parent() {
137+
}
138+
139+
public Parent(String name) {
140+
this.name = name;
141+
}
142+
143+
public abstract Long getId();
144+
145+
public String getName() {
146+
return name;
147+
}
148+
149+
public List<C> getChildren() {
150+
return children;
151+
}
152+
}
153+
154+
@Entity( name = "ChildA" )
155+
public static class ChildA extends Child<ParentA> {
156+
public ChildA() {
157+
}
158+
159+
public ChildA(String name, ParentA parent) {
160+
super( name, parent );
161+
}
162+
}
163+
164+
@Entity( name = "ChildB" )
165+
public static class ChildB extends Child<ParentB> {
166+
public ChildB() {
167+
}
168+
169+
public ChildB(String name, ParentB parent) {
170+
super( name, parent );
171+
}
172+
}
173+
174+
@Entity( name = "ParentA" )
175+
public static class ParentA extends Parent<ChildA> {
176+
@Id
177+
private Long id;
178+
179+
public ParentA() {
180+
}
181+
182+
public ParentA(Long id, String name) {
183+
super( name );
184+
this.id = id;
185+
}
186+
187+
@Override
188+
public Long getId() {
189+
return id;
190+
}
191+
}
192+
193+
@Entity( name = "ParentB" )
194+
public static class ParentB extends Parent<ChildB> {
195+
@Id
196+
private Long id;
197+
198+
public ParentB() {
199+
}
200+
201+
public ParentB(Long id, String name) {
202+
super( name );
203+
this.id = id;
204+
}
205+
206+
@Override
207+
public Long getId() {
208+
return id;
209+
}
210+
}
211+
}

0 commit comments

Comments
 (0)