6
6
*/
7
7
package org .hibernate .orm .test .proxy ;
8
8
9
+ import java .util .List ;
10
+
9
11
import org .hibernate .Hibernate ;
10
12
11
13
import org .hibernate .testing .orm .junit .DomainModel ;
23
25
24
26
import static org .assertj .core .api .Assertions .assertThat ;
25
27
26
- @ DomainModel ( annotatedClasses = {
28
+ @ DomainModel (annotatedClasses = {
27
29
ProxyAsQueryParameterTest .Product .class ,
28
30
ProxyAsQueryParameterTest .Vendor .class ,
29
31
ProxyAsQueryParameterTest .CarVendor .class ,
32
+ ProxyAsQueryParameterTest .LuxuryCarVendor .class ,
30
33
ProxyAsQueryParameterTest .Producer .class ,
31
- } )
34
+ })
32
35
@ SessionFactory
33
- @ Jira ( "https://hibernate.atlassian.net/browse/HHH-17467" )
36
+ @ Jira ("https://hibernate.atlassian.net/browse/HHH-17467" )
34
37
public class ProxyAsQueryParameterTest {
38
+
39
+ private static final Integer PRODUCT_ID = 1 ;
40
+ private static final Integer LUXURY_PRODUCT_ID = 2 ;
41
+
35
42
@ BeforeAll
36
43
public void setUp (SessionFactoryScope scope ) {
37
44
scope .inTransaction ( session -> {
38
45
final CarVendor vendor = new CarVendor ( 1 , "vendor_1" , "dealership_1" );
39
46
session .persist ( vendor );
40
47
final Producer producer = new Producer ( 1 , "producer_1" );
41
48
session .persist ( producer );
42
- final Product product = new Product ( 1 , vendor , producer );
49
+ final Product product = new Product ( PRODUCT_ID , vendor , producer );
43
50
session .persist ( product );
51
+
52
+ final LuxuryCarVendor luxuryCarVendor = new LuxuryCarVendor ( 2 , "vendor_2" , "luxury" );
53
+ session .persist ( luxuryCarVendor );
54
+ final Product luxuryProduct = new Product ( LUXURY_PRODUCT_ID , luxuryCarVendor , producer );
55
+ session .persist ( luxuryProduct );
44
56
} );
45
57
}
46
58
@@ -55,49 +67,66 @@ public void tearDown(SessionFactoryScope scope) {
55
67
@ Test
56
68
public void testProxyParam (SessionFactoryScope scope ) {
57
69
scope .inTransaction ( session -> {
58
- final Product product = session .createQuery ( "from Product p" , Product .class ).getSingleResult ();
70
+ final Product product = session .createQuery ( "from Product p where p.id = :productId" , Product .class )
71
+ .setParameter ( "productId" , PRODUCT_ID )
72
+ .getSingleResult ();
59
73
assertThat ( Hibernate .isInitialized ( product .getProducer () ) ).isFalse ();
60
- final Product result = session .createQuery (
74
+ final List < Product > results = session .createQuery (
61
75
"from Product p where p.producer = :producer" ,
62
76
Product .class
63
- ).setParameter ( "producer" , product .getProducer () ).getSingleResult ();
64
- // The proxy should not have been initialized since Producer doesn't have subclasses
65
- assertThat ( Hibernate . isInitialized ( product .getProducer () ) ). isFalse ( );
66
- assertThat ( result .getProducer ().getId () ).isEqualTo ( product .getProducer ().getId () );
77
+ ).setParameter ( "producer" , product .getProducer () ).getResultList ();
78
+ assertThat ( results . size () ). isEqualTo ( 2 );
79
+ assertThat ( results . get ( 0 ) .getProducer (). getId () ). isEqualTo ( product . getProducer (). getId () );
80
+ assertThat ( results . get ( 1 ) .getProducer ().getId () ).isEqualTo ( product .getProducer ().getId () );
67
81
} );
68
82
}
69
83
70
84
@ Test
71
85
public void testProxyParamWithSubclasses (SessionFactoryScope scope ) {
72
86
scope .inTransaction ( session -> {
73
- final Product product = session .createQuery ( "from Product p" , Product .class ).getSingleResult ();
87
+ final Product product = session .createQuery ( "from Product p where p.id = :productId" , Product .class )
88
+ .setParameter ( "productId" , PRODUCT_ID )
89
+ .getSingleResult ();
74
90
assertThat ( Hibernate .isInitialized ( product .getVendor () ) ).isFalse ();
75
91
final Product result = session .createQuery (
76
92
"from Product p where p.vendor = :vendor" ,
77
93
Product .class
78
94
).setParameter ( "vendor" , product .getVendor () ).getSingleResult ();
79
- // The proxy will have been initialized since Vendor has subclasses
80
- assertThat ( Hibernate .isInitialized ( product .getVendor () ) ).isTrue ();
81
95
assertThat ( result .getVendor ().getId () ).isEqualTo ( product .getVendor ().getId () );
82
96
} );
83
97
}
84
98
85
99
@ Test
86
100
public void testSubclassProxyParam (SessionFactoryScope scope ) {
87
101
scope .inTransaction ( session -> {
88
- final Product product = session .createQuery ( "from Product p" , Product .class ).getSingleResult ();
102
+ final Product product = session .createQuery ( "from Product p where p.id = :productId" , Product .class )
103
+ .setParameter ( "productId" , PRODUCT_ID )
104
+ .getSingleResult ();
89
105
assertThat ( Hibernate .isInitialized ( product .getVendor () ) ).isFalse ();
90
106
final CarVendor result = session .createQuery (
91
107
"from CarVendor v where v = :vendor" ,
92
108
CarVendor .class
93
109
).setParameter ( "vendor" , product .getVendor () ).getSingleResult ();
94
- // The proxy should have been initialized since Vendor has subclasses
95
- assertThat ( Hibernate .isInitialized ( product .getVendor () ) ).isTrue ();
96
110
assertThat ( result .getId () ).isEqualTo ( product .getVendor ().getId () );
97
111
} );
98
112
}
99
113
100
- @ Entity ( name = "Producer" )
114
+ @ Test
115
+ public void testSubSubclassProxyParam (SessionFactoryScope scope ) {
116
+ scope .inTransaction ( session -> {
117
+ final Product product = session .createQuery ( "from Product p where p.id = :productId" , Product .class )
118
+ .setParameter ( "productId" , LUXURY_PRODUCT_ID )
119
+ .getSingleResult ();
120
+ assertThat ( Hibernate .isInitialized ( product .getVendor () ) ).isFalse ();
121
+ final LuxuryCarVendor result = session .createQuery (
122
+ "from CarVendor v where v = :vendor" ,
123
+ LuxuryCarVendor .class
124
+ ).setParameter ( "vendor" , product .getVendor () ).getSingleResult ();
125
+ assertThat ( result .getId () ).isEqualTo ( product .getVendor ().getId () );
126
+ } );
127
+ }
128
+
129
+ @ Entity (name = "Producer" )
101
130
public static class Producer {
102
131
@ Id
103
132
private Integer id ;
@@ -120,7 +149,7 @@ public String getName() {
120
149
}
121
150
}
122
151
123
- @ Entity ( name = "Vendor" )
152
+ @ Entity (name = "Vendor" )
124
153
public static class Vendor {
125
154
@ Id
126
155
private Integer id ;
@@ -143,7 +172,7 @@ public String getName() {
143
172
}
144
173
}
145
174
146
- @ Entity ( name = "CarVendor" )
175
+ @ Entity (name = "CarVendor" )
147
176
public static class CarVendor extends Vendor {
148
177
private String dealership ;
149
178
@@ -160,7 +189,18 @@ public String getDealership() {
160
189
}
161
190
}
162
191
163
- @ Entity ( name = "Product" )
192
+ @ Entity (name = "LuxuryCarVendor" )
193
+ public static class LuxuryCarVendor extends CarVendor {
194
+
195
+ public LuxuryCarVendor () {
196
+ }
197
+
198
+ public LuxuryCarVendor (int id , String name , String dealership ) {
199
+ super ( id , name , dealership );
200
+ }
201
+ }
202
+
203
+ @ Entity (name = "Product" )
164
204
public static final class Product {
165
205
private Integer id ;
166
206
private Vendor vendor ;
@@ -184,7 +224,7 @@ public void setId(Integer id) {
184
224
this .id = id ;
185
225
}
186
226
187
- @ ManyToOne ( fetch = FetchType .LAZY )
227
+ @ ManyToOne (fetch = FetchType .LAZY )
188
228
public Vendor getVendor () {
189
229
return vendor ;
190
230
}
@@ -193,7 +233,7 @@ public void setVendor(Vendor vendor) {
193
233
this .vendor = vendor ;
194
234
}
195
235
196
- @ ManyToOne ( fetch = FetchType .LAZY )
236
+ @ ManyToOne (fetch = FetchType .LAZY )
197
237
public Producer getProducer () {
198
238
return producer ;
199
239
}
0 commit comments