Skip to content

Commit 6d174d3

Browse files
committed
1 parent 0c72972 commit 6d174d3

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.hibernate.orm.test.joinsubquery;
2+
3+
import org.hibernate.testing.orm.junit.DomainModel;
4+
import org.hibernate.testing.orm.junit.JiraKey;
5+
import org.hibernate.testing.orm.junit.SessionFactory;
6+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
9+
10+
import static org.junit.jupiter.api.Assertions.assertNotNull;
11+
12+
@DomainModel(annotatedClasses = { RecordItem.class, RecordType.class })
13+
@SessionFactory
14+
@JiraKey("HHH-19052")
15+
class JoinSubqueryTest {
16+
17+
@BeforeAll
18+
static void setUp(SessionFactoryScope scope) throws Exception {
19+
scope.inTransaction( session -> {
20+
final var id = 1L;
21+
final var typeId = 42L;
22+
final var recordType = new RecordType( id, typeId );
23+
session.persist( recordType );
24+
final var item = new RecordItem( id, typeId, recordType );
25+
session.persist( item );
26+
} );
27+
}
28+
29+
@Test
30+
void test(SessionFactoryScope scope) throws Exception {
31+
scope.inSession( session -> {
32+
final var item = session.get( RecordItem.class, 1L );
33+
assertNotNull( item );
34+
} );
35+
}
36+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.hibernate.orm.test.joinsubquery;
2+
3+
import java.io.Serializable;
4+
5+
import org.hibernate.annotations.JoinColumnOrFormula;
6+
import org.hibernate.annotations.JoinFormula;
7+
8+
import jakarta.persistence.Column;
9+
import jakarta.persistence.Entity;
10+
import jakarta.persistence.FetchType;
11+
import jakarta.persistence.Id;
12+
import jakarta.persistence.JoinColumn;
13+
import jakarta.persistence.ManyToOne;
14+
import jakarta.persistence.Table;
15+
16+
@Entity
17+
@Table(name = "record_items")
18+
public class RecordItem implements Serializable {
19+
20+
@Id
21+
protected Long id;
22+
23+
@Column(name = "type_id", insertable = false, updatable = false)
24+
private Long typeId;
25+
26+
@ManyToOne(fetch = FetchType.EAGER)
27+
@JoinColumnOrFormula(column = @JoinColumn(name = "type_id", referencedColumnName = "entity_id"))
28+
@JoinColumnOrFormula(formula = @JoinFormula(value = "(SELECT x.id FROM record_types x WHERE x.entity_id = type_id)", referencedColumnName = "id"))
29+
private RecordType type;
30+
31+
RecordItem() {
32+
}
33+
34+
public RecordItem(Long id, Long typeId, RecordType type) {
35+
this.id = id;
36+
this.typeId = typeId;
37+
this.type = type;
38+
}
39+
40+
public void setId(Long id) {
41+
this.id = id;
42+
}
43+
44+
public Long getId() {
45+
return this.id;
46+
}
47+
48+
public Long getTypeId() {
49+
return typeId;
50+
}
51+
52+
public RecordType getType() {
53+
return type;
54+
}
55+
56+
57+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.hibernate.orm.test.joinsubquery;
2+
3+
import java.io.Serializable;
4+
5+
import jakarta.persistence.Column;
6+
import jakarta.persistence.Entity;
7+
import jakarta.persistence.Id;
8+
import jakarta.persistence.Table;
9+
10+
@Entity
11+
@Table(name = "record_types")
12+
public class RecordType implements Serializable {
13+
14+
@Id
15+
protected Long id;
16+
17+
@Column(name = "entity_id")
18+
private Long entityId;
19+
20+
RecordType() {
21+
}
22+
23+
public RecordType(Long id, Long entityId) {
24+
this.id = id;
25+
this.entityId = entityId;
26+
}
27+
28+
public void setId(Long id) {
29+
this.id = id;
30+
}
31+
32+
public Long getId() {
33+
return this.id;
34+
}
35+
36+
public Long getEntityId() {
37+
return entityId;
38+
}
39+
40+
public void setEntityId(Long entityId) {
41+
this.entityId = entityId;
42+
}
43+
44+
}

0 commit comments

Comments
 (0)