Skip to content

Commit f283df2

Browse files
committed
HHH-19031 Add test for issue
1 parent 94acfa3 commit f283df2

File tree

2 files changed

+440
-0
lines changed

2 files changed

+440
-0
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.jpa.query;
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.FetchType;
12+
import jakarta.persistence.Id;
13+
import jakarta.persistence.JoinColumn;
14+
import jakarta.persistence.JoinColumns;
15+
import jakarta.persistence.ManyToOne;
16+
import jakarta.persistence.Table;
17+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
18+
import org.hibernate.testing.orm.junit.JiraKey;
19+
import org.hibernate.testing.orm.junit.Jpa;
20+
import org.junit.jupiter.api.BeforeAll;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.util.List;
24+
25+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
26+
27+
@Jpa(
28+
annotatedClasses = {
29+
CompositeIdRepeatedQueryTest.Corporation.class,
30+
CompositeIdRepeatedQueryTest.CorporationUser.class,
31+
CompositeIdRepeatedQueryTest.Document.class,
32+
CompositeIdRepeatedQueryTest.Person.class,
33+
}
34+
)
35+
@JiraKey( "HHH-19031" )
36+
public class CompositeIdRepeatedQueryTest {
37+
38+
@BeforeAll
39+
public void setup(EntityManagerFactoryScope scope) {
40+
scope.inTransaction(
41+
entityManager -> {
42+
Person person = new Person("1", "Andrew");
43+
Corporation corporation = new Corporation("2", "ibm");
44+
Document document = new Document(3,"registration", person, corporation);
45+
entityManager.persist( person );
46+
entityManager.persist( corporation );
47+
entityManager.persist( document );
48+
}
49+
);
50+
}
51+
52+
@Test
53+
public void testRepeatedQuery(EntityManagerFactoryScope scope){
54+
scope.inTransaction(
55+
entityManager -> {
56+
String query = "SELECT d FROM Document d";
57+
58+
List<Document> resultList = entityManager.createQuery( query, Document.class ).getResultList();
59+
assertThat(resultList.size()).isEqualTo( 1 );
60+
Document document = resultList.get( 0 );
61+
62+
resultList = entityManager.createQuery( query, Document.class ).getResultList();
63+
assertThat(resultList.size()).isEqualTo( 1 );
64+
assertThat( resultList.get( 0 ) ).isSameAs( document );
65+
}
66+
);
67+
}
68+
69+
@Entity(name = "Document")
70+
@Table(name = "documents")
71+
public static class Document {
72+
@Id
73+
private Integer id;
74+
75+
@Embedded
76+
private Owner owner;
77+
78+
private String name;
79+
80+
public Document() {
81+
}
82+
83+
public Document(Integer id, String name, Person person, Corporation corporation) {
84+
this.id = id;
85+
this.name = name;
86+
owner = new Owner(person, corporation);
87+
}
88+
89+
public Integer getId() {
90+
return id;
91+
}
92+
93+
public Owner getOwner() {
94+
return owner;
95+
}
96+
}
97+
98+
@Embeddable
99+
public static class Owner {
100+
@ManyToOne(fetch = FetchType.LAZY)
101+
@JoinColumn(name = "owner")
102+
private Person person;
103+
104+
@ManyToOne(fetch = FetchType.LAZY)
105+
@JoinColumn(name = "owner_corporation")
106+
private Corporation corporation;
107+
108+
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
109+
@JoinColumns({
110+
@JoinColumn(name = "owner_corporation", referencedColumnName = "id_corporation", insertable = false, updatable = false),
111+
@JoinColumn(name = "owner", referencedColumnName = "id_person", insertable = false, updatable = false),
112+
})
113+
private CorporationUser corporationUser;
114+
115+
public Owner() {
116+
}
117+
118+
public Owner(Person person, Corporation corporation) {
119+
this.person = person;
120+
this.corporation = corporation;
121+
this.corporationUser = new CorporationUser(person, corporation) ;
122+
}
123+
124+
public Person getPerson() {
125+
return person;
126+
}
127+
128+
public Corporation getCorporation() {
129+
return corporation;
130+
}
131+
132+
public CorporationUser getCorporationUser() {
133+
return corporationUser;
134+
}
135+
}
136+
137+
@Entity(name = "Corporation")
138+
@Table(name = "corporation")
139+
public static class Corporation {
140+
@Id
141+
private String id;
142+
143+
private String corporateName;
144+
145+
public Corporation() {
146+
}
147+
148+
public Corporation(String id, String corporateName) {
149+
this.id = id;
150+
this.corporateName = corporateName;
151+
}
152+
153+
public String getId() {
154+
return id;
155+
}
156+
157+
public String getCorporateName() {
158+
return corporateName;
159+
}
160+
}
161+
162+
@Entity(name = "CorporationUser")
163+
@Table(name = "corporation_person_xref")
164+
public static class CorporationUser {
165+
@Id
166+
@ManyToOne
167+
@JoinColumn(name = "id_person", updatable = false)
168+
private Person person;
169+
170+
@Id
171+
@ManyToOne
172+
@JoinColumn(name = "id_corporation", updatable = false)
173+
private Corporation corporation;
174+
175+
public CorporationUser() {
176+
}
177+
178+
public CorporationUser(Person person, Corporation corporation) {
179+
this.person = person;
180+
this.corporation = corporation;
181+
}
182+
183+
public Person getPerson() {
184+
return person;
185+
}
186+
187+
public Corporation getCorporation() {
188+
return corporation;
189+
}
190+
}
191+
192+
@Entity(name = "Person")
193+
@Table(name = "person")
194+
public static class Person {
195+
@Id
196+
private String id;
197+
198+
private String surname;
199+
200+
public Person() {
201+
}
202+
203+
public Person(String id, String surname) {
204+
this.id = id;
205+
this.surname = surname;
206+
}
207+
208+
public String getId() {
209+
return id;
210+
}
211+
212+
public String getSurname() {
213+
return surname;
214+
}
215+
}
216+
}

0 commit comments

Comments
 (0)