/
CompositeIdTest.java
executable file
·379 lines (330 loc) · 11.9 KB
/
CompositeIdTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2006-2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.cid;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.engine.query.spi.HQLQueryPlan;
import org.hibernate.exception.SQLGrammarException;
import org.hibernate.hql.spi.QueryTranslator;
import org.hibernate.testing.FailureExpectedWithNewUnifiedXsd;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* @author Gavin King
*/
public class CompositeIdTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "cid/Customer.hbm.xml", "cid/Order.hbm.xml", "cid/LineItem.hbm.xml", "cid/Product.hbm.xml" };
}
@Test
public void testNonDistinctCountOfEntityWithCompositeId() {
// the check here is all based on whether we had commas in the expressions inside the count
final HQLQueryPlan plan = sessionFactory().getQueryPlanCache().getHQLQueryPlan(
"select count(o) from Order o",
false,
Collections.EMPTY_MAP
);
assertEquals( 1, plan.getTranslators().length );
final QueryTranslator translator = plan.getTranslators()[0];
final String generatedSql = translator.getSQLString();
final int countExpressionListStart = generatedSql.indexOf( "count(" );
final int countExpressionListEnd = generatedSql.indexOf( ")", countExpressionListStart );
final String countExpressionFragment = generatedSql.substring( countExpressionListStart+6, countExpressionListEnd+1 );
final boolean hadCommas = countExpressionFragment.contains( "," );
// set up the expectation based on Dialect...
final boolean expectCommas = sessionFactory().getDialect().supportsTupleCounts();
assertEquals( expectCommas, hadCommas );
}
@Test
public void testDistinctCountOfEntityWithCompositeId() {
// today we do not account for Dialects supportsTupleDistinctCounts() is false. though really the only
// "option" there is to throw an error.
final HQLQueryPlan plan = sessionFactory().getQueryPlanCache().getHQLQueryPlan(
"select count(distinct o) from Order o",
false,
Collections.EMPTY_MAP
);
assertEquals( 1, plan.getTranslators().length );
final QueryTranslator translator = plan.getTranslators()[0];
final String generatedSql = translator.getSQLString();
System.out.println( "Generated SQL : " + generatedSql );
final int countExpressionListStart = generatedSql.indexOf( "count(" );
final int countExpressionListEnd = generatedSql.indexOf( ")", countExpressionListStart );
final String countExpressionFragment = generatedSql.substring( countExpressionListStart+6, countExpressionListEnd+1 );
assertTrue( countExpressionFragment.startsWith( "distinct" ) );
assertTrue( countExpressionFragment.contains( "," ) );
Session s = openSession();
s.beginTransaction();
Customer c = new Customer();
c.setCustomerId( "1" );
c.setAddress("123 somewhere");
c.setName("Brett");
Order o1 = new Order( c );
o1.setOrderDate( Calendar.getInstance() );
Order o2 = new Order( c );
o2.setOrderDate( Calendar.getInstance() );
s.persist( c );
s.persist( o1 );
s.persist( o2 );
s.getTransaction().commit();
s.clear();
s.beginTransaction();
try {
long count = ( Long ) s.createQuery( "select count(distinct o) FROM Order o" ).uniqueResult();
if ( ! getDialect().supportsTupleDistinctCounts() ) {
fail( "expected SQLGrammarException" );
}
assertEquals( 2l, count );
}
catch ( SQLGrammarException e ) {
if ( getDialect().supportsTupleDistinctCounts() ) {
throw e;
}
}
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.createQuery("delete from Order").executeUpdate();
s.createQuery("delete from Customer").executeUpdate();
s.getTransaction().commit();
s.close();
}
@Test
public void testQuery() {
Session s = openSession();
Transaction t = s.beginTransaction();
s.createQuery("from LineItem ol where ol.order.id.customerId = 'C111'").list();
t.commit();
s.close();
}
@Test
@FailureExpectedWithNewUnifiedXsd(message = "MultipleBagFetchException")
public void testCompositeIds() {
Session s = openSession();
Transaction t = s.beginTransaction();
Product p = new Product();
p.setProductId("A123");
p.setDescription("nipple ring");
p.setPrice( new BigDecimal(1.0) );
p.setNumberAvailable(1004);
s.persist(p);
Product p2 = new Product();
p2.setProductId("X525");
p2.setDescription("nose stud");
p2.setPrice( new BigDecimal(3.0) );
p2.setNumberAvailable(105);
s.persist(p2);
Customer c = new Customer();
c.setAddress("St Kilda Rd, MEL, 3000");
c.setName("Virginia");
c.setCustomerId("C111");
s.persist(c);
Order o = new Order(c);
o.setOrderDate( Calendar.getInstance() );
LineItem li = new LineItem(o, p);
li.setQuantity(2);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
o = (Order) s.get( Order.class, new Order.Id("C111", 0) );
assertEquals( o.getTotal().intValue(), 2 );
o.getCustomer().getName();
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.createQuery("from Customer c left join fetch c.orders o left join fetch o.lineItems li left join fetch li.product p").list();
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.createQuery("from Order o left join fetch o.lineItems li left join fetch li.product p").list();
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
Iterator iter = s.createQuery("select o.id, li.id from Order o join o.lineItems li").list().iterator();
while ( iter.hasNext() ) {
Object[] stuff = (Object[]) iter.next();
assertTrue(stuff.length==2);
}
iter = s.createQuery("from Order o join o.lineItems li").iterate();
while ( iter.hasNext() ) {
Object[] stuff = (Object[]) iter.next();
assertTrue(stuff.length==2);
}
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
c = (Customer) s.get( Customer.class, "C111" );
Order o2 = new Order(c);
o2.setOrderDate( Calendar.getInstance() );
s.flush();
LineItem li2 = new LineItem(o2, p2);
li2.setQuantity(5);
List bigOrders = s.createQuery("from Order o where o.total>10.0").list();
assertEquals( bigOrders.size(), 1 );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.createQuery("delete from LineItem").executeUpdate();
s.createQuery("delete from Order").executeUpdate();
s.createQuery("delete from Customer").executeUpdate();
s.createQuery("delete from Product").executeUpdate();
t.commit();
s.close();
}
@Test
public void testNonLazyFetch() {
Session s = openSession();
Transaction t = s.beginTransaction();
Product p = new Product();
p.setProductId("A123");
p.setDescription("nipple ring");
p.setPrice( new BigDecimal(1.0) );
p.setNumberAvailable(1004);
s.persist(p);
Product p2 = new Product();
p2.setProductId("X525");
p2.setDescription("nose stud");
p2.setPrice( new BigDecimal(3.0) );
p2.setNumberAvailable(105);
s.persist(p2);
Customer c = new Customer();
c.setAddress("St Kilda Rd, MEL, 3000");
c.setName("Virginia");
c.setCustomerId("C111");
s.persist(c);
Order o = new Order(c);
o.setOrderDate( Calendar.getInstance() );
LineItem li = new LineItem(o, p);
li.setQuantity(2);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
o = (Order) s.get( Order.class, new Order.Id("C111", 0) );
assertEquals( o.getTotal().intValue(), 2 );
o.getCustomer().getName();
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
o = (Order) s.createQuery("from Order o left join fetch o.lineItems li left join fetch li.product p").uniqueResult();
assertTrue( Hibernate.isInitialized( o.getLineItems() ) );
li = (LineItem) o.getLineItems().iterator().next();
assertTrue( Hibernate.isInitialized( li ) );
assertTrue( Hibernate.isInitialized( li.getProduct() ) );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
o = (Order) s.createQuery("from Order o").uniqueResult();
assertTrue( Hibernate.isInitialized( o.getLineItems() ) );
li = (LineItem) o.getLineItems().iterator().next();
assertTrue( Hibernate.isInitialized( li ) );
assertFalse( Hibernate.isInitialized( li.getProduct() ) );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.createQuery("delete from LineItem").executeUpdate();
s.createQuery("delete from Order").executeUpdate();
s.createQuery("delete from Customer").executeUpdate();
s.createQuery("delete from Product").executeUpdate();
t.commit();
s.close();
}
@Test
@FailureExpectedWithNewUnifiedXsd(message = "MultipleBagFetchException")
public void testMultipleCollectionFetch() {
Session s = openSession();
Transaction t = s.beginTransaction();
Product p = new Product();
p.setProductId("A123");
p.setDescription("nipple ring");
p.setPrice( new BigDecimal(1.0) );
p.setNumberAvailable(1004);
s.persist(p);
Product p2 = new Product();
p2.setProductId("X525");
p2.setDescription("nose stud");
p2.setPrice( new BigDecimal(3.0) );
p2.setNumberAvailable(105);
s.persist(p2);
Customer c = new Customer();
c.setAddress("St Kilda Rd, MEL, 3000");
c.setName("Virginia");
c.setCustomerId("C111");
s.persist(c);
Order o = new Order(c);
o.setOrderDate( Calendar.getInstance() );
LineItem li = new LineItem(o, p);
li.setQuantity(2);
LineItem li2 = new LineItem(o, p2);
li2.setQuantity(3);
Order o2 = new Order(c);
o2.setOrderDate( Calendar.getInstance() );
LineItem li3 = new LineItem(o2, p);
li3.setQuantity(1);
LineItem li4 = new LineItem(o2, p2);
li4.setQuantity(1);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
c = (Customer) s.createQuery("from Customer c left join fetch c.orders o left join fetch o.lineItems li left join fetch li.product p").uniqueResult();
assertTrue( Hibernate.isInitialized( c.getOrders() ) );
assertEquals( c.getOrders().size(), 2 );
assertTrue( Hibernate.isInitialized( ( (Order) c.getOrders().get(0) ).getLineItems() ) );
assertTrue( Hibernate.isInitialized( ( (Order) c.getOrders().get(1) ).getLineItems() ) );
assertEquals( ( (Order) c.getOrders().get(0) ).getLineItems().size(), 2 );
assertEquals( ( (Order) c.getOrders().get(1) ).getLineItems().size(), 2 );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.createQuery("delete from LineItem").executeUpdate();
s.createQuery("delete from Order").executeUpdate();
s.createQuery("delete from Customer").executeUpdate();
s.createQuery("delete from Product").executeUpdate();
t.commit();
s.close();
}
}