File tree Expand file tree Collapse file tree 3 files changed +132
-0
lines changed
hibernate-core/src/test/java/org/hibernate/orm/test/joinsubquery Expand file tree Collapse file tree 3 files changed +132
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package org .hibernate .orm .test .joinsubquery ;
2+
3+ import jakarta .persistence .*;
4+ import org .hibernate .annotations .JoinColumnOrFormula ;
5+ import org .hibernate .annotations .JoinFormula ;
6+
7+ import java .io .Serializable ;
8+
9+ @ Entity
10+ @ Table (name = "record_items" )
11+ public class RecordItem implements Serializable {
12+
13+ @ Id
14+ //@GeneratedValue(strategy = GenerationType.IDENTITY)
15+ protected Long id ;
16+
17+ @ Column (name = "type_id" , insertable = false , updatable = false )
18+ private Long typeId ;
19+
20+ @ ManyToOne (fetch = FetchType .EAGER )
21+ @ JoinColumnOrFormula (column = @ JoinColumn (name = "type_id" , referencedColumnName = "entity_id" ))
22+ @ JoinColumnOrFormula (formula = @ JoinFormula (value = "(SELECT x.id FROM record_types x WHERE x.entity_id = type_id)" , referencedColumnName = "id" ))
23+ private RecordType type ;
24+
25+ RecordItem () {
26+ }
27+
28+ public RecordItem (Long id , Long typeId , RecordType type ) {
29+ this .id = id ;
30+ this .typeId = typeId ;
31+ this .type = type ;
32+ }
33+
34+ public void setId (Long id ) {
35+ this .id = id ;
36+ }
37+
38+ public Long getId () {
39+ return this .id ;
40+ }
41+
42+ public Long getTypeId () {
43+ return typeId ;
44+ }
45+
46+ public RecordType getType () {
47+ return type ;
48+ }
49+
50+
51+ }
Original file line number Diff line number Diff line change 1+ package org .hibernate .orm .test .joinsubquery ;
2+
3+ import jakarta .persistence .Column ;
4+ import jakarta .persistence .Entity ;
5+ import jakarta .persistence .Id ;
6+ import jakarta .persistence .Table ;
7+
8+ import java .io .Serializable ;
9+
10+ @ Entity
11+ @ Table (name = "record_types" )
12+ public class RecordType implements Serializable {
13+
14+ @ Id
15+ // @GeneratedValue(strategy = GenerationType.IDENTITY)
16+ protected Long id ;
17+
18+ @ Column (name = "entity_id" )
19+ private Long entityId ;
20+
21+ RecordType () {
22+ }
23+
24+ public RecordType (Long id , Long entityId ) {
25+ this .id = id ;
26+ this .entityId = entityId ;
27+ }
28+
29+ public void setId (Long id ) {
30+ this .id = id ;
31+ }
32+
33+ public Long getId () {
34+ return this .id ;
35+ }
36+
37+ public Long getEntityId () {
38+ return entityId ;
39+ }
40+
41+ public void setEntityId (Long entityId ) {
42+ this .entityId = entityId ;
43+ }
44+
45+ }
You can’t perform that action at this time.
0 commit comments