Skip to content

Commit d259836

Browse files
cigalybeikov
authored andcommitted
1 parent e54b125 commit d259836

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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.joinsubquery;
8+
9+
import jakarta.persistence.*;
10+
import org.hibernate.annotations.JoinColumnOrFormula;
11+
import org.hibernate.annotations.JoinFormula;
12+
import org.hibernate.testing.orm.junit.DomainModel;
13+
import org.hibernate.testing.orm.junit.JiraKey;
14+
import org.hibernate.testing.orm.junit.SessionFactory;
15+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
16+
import org.junit.jupiter.api.BeforeAll;
17+
import org.junit.jupiter.api.Test;
18+
19+
import java.io.Serializable;
20+
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
23+
@DomainModel(annotatedClasses = {
24+
JoinSubqueryTest.RecordItem.class,
25+
JoinSubqueryTest.RecordType.class
26+
})
27+
@SessionFactory
28+
@JiraKey("HHH-19052")
29+
class JoinSubqueryTest {
30+
31+
@BeforeAll
32+
static void setUp(SessionFactoryScope scope) throws Exception {
33+
scope.inTransaction(session -> {
34+
final var id = 1L;
35+
final var typeId = 42L;
36+
final var recordType = new RecordType(id, typeId);
37+
session.persist(recordType);
38+
final var item = new RecordItem(id, typeId, recordType);
39+
session.persist(item);
40+
});
41+
}
42+
43+
@Test
44+
void test(SessionFactoryScope scope) throws Exception {
45+
scope.inSession(session -> {
46+
final var item = session.get(RecordItem.class, 1L);
47+
assertNotNull(item);
48+
});
49+
}
50+
51+
@Entity
52+
@Table(name = "record_items")
53+
public static class RecordItem implements Serializable {
54+
55+
@Id
56+
protected Long id;
57+
58+
@Column(name = "type_id", insertable = false, updatable = false)
59+
private Long typeId;
60+
61+
@ManyToOne(fetch = FetchType.EAGER)
62+
@JoinColumnOrFormula(column = @JoinColumn(name = "type_id", referencedColumnName = "entity_id"))
63+
@JoinColumnOrFormula(formula = @JoinFormula(value = "(SELECT x.id FROM record_types x WHERE x.entity_id = type_id)", referencedColumnName = "id"))
64+
private RecordType type;
65+
66+
RecordItem() {
67+
}
68+
69+
public RecordItem(Long id, Long typeId, RecordType type) {
70+
this.id = id;
71+
this.typeId = typeId;
72+
this.type = type;
73+
}
74+
75+
public void setId(Long id) {
76+
this.id = id;
77+
}
78+
79+
public Long getId() {
80+
return this.id;
81+
}
82+
83+
public Long getTypeId() {
84+
return typeId;
85+
}
86+
87+
public RecordType getType() {
88+
return type;
89+
}
90+
91+
92+
}
93+
94+
@Entity
95+
@Table(name = "record_types")
96+
public static class RecordType implements Serializable {
97+
98+
@Id
99+
protected Long id;
100+
101+
@Column(name = "entity_id")
102+
private Long entityId;
103+
104+
RecordType() {
105+
}
106+
107+
public RecordType(Long id, Long entityId) {
108+
this.id = id;
109+
this.entityId = entityId;
110+
}
111+
112+
public void setId(Long id) {
113+
this.id = id;
114+
}
115+
116+
public Long getId() {
117+
return this.id;
118+
}
119+
120+
public Long getEntityId() {
121+
return entityId;
122+
}
123+
124+
public void setEntityId(Long entityId) {
125+
this.entityId = entityId;
126+
}
127+
128+
}
129+
}

0 commit comments

Comments
 (0)