Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 16312a2704889be9016b0f3a7930043e34a10bc8 (plain) (blame)
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
 * Copyright (c) 2009-2013, 2015 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Martin Taal
 */
package org.eclipse.emf.cdo.tests.hibernate;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.server.hibernate.IHibernateStore;
import org.eclipse.emf.cdo.server.internal.hibernate.HibernateUtil;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.AbstractCDOTest;
import org.eclipse.emf.cdo.tests.model1.Customer;
import org.eclipse.emf.cdo.tests.model1.Order;
import org.eclipse.emf.cdo.tests.model1.OrderDetail;
import org.eclipse.emf.cdo.tests.model1.Product1;
import org.eclipse.emf.cdo.tests.model1.SalesOrder;
import org.eclipse.emf.cdo.tests.model1.Supplier;
import org.eclipse.emf.cdo.tests.model1.VAT;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.cdo.view.CDOQuery;

import org.eclipse.net4j.util.WrappedException;
import org.eclipse.net4j.util.collection.CloseableIterator;

import java.util.ArrayList;
import java.util.List;

/**
 * Test different aspects of HQL querying using the CDO query api.
 *
 * @author Martin Taal
 */
public class HibernateQueryTest extends AbstractCDOTest
{
  private static final int NUM_OF_PRODUCTS = 20;

  private static final int NUM_OF_CUSTOMERS = 5;

  private static final int NUM_OF_PRODUCTS_CUSTOMER = NUM_OF_PRODUCTS / NUM_OF_CUSTOMERS;

  private static final int NUM_OF_SALES_ORDERS = 5;

  private static final int NUM_OF_SUPPLIERS = 5;

  @Override
  public void doSetUp() throws Exception
  {
    org.eclipse.emf.cdo.tests.model1.Model1Package.eINSTANCE.getSupplier_Preferred().setLowerBound(1);
    org.eclipse.emf.cdo.tests.model1.legacy.Model1Package.eINSTANCE.getSupplier_Preferred().setLowerBound(1);
    super.doSetUp();
  }

  @Override
  public void doTearDown() throws Exception
  {
    org.eclipse.emf.cdo.tests.model1.Model1Package.eINSTANCE.getSupplier_Preferred().setLowerBound(0);
    org.eclipse.emf.cdo.tests.model1.legacy.Model1Package.eINSTANCE.getSupplier_Preferred().setLowerBound(0);
    super.doTearDown();
  }

  public void testSimpleQueries() throws Exception
  {
    msg("Opening session");
    CDOSession session = openSession();

    createTestSet(session);

    session.close();
    session = openSession();
    msg("Opening transaction for querying");
    CDOTransaction transaction = session.openTransaction();

    clearCache(getRepository().getRevisionManager());

    {
      msg("Query for products");
      CDOQuery cdoQuery = transaction.createQuery("hql", "from Product1");
      addCacheParameter(cdoQuery);
      final List<Product1> products = cdoQuery.getResult(Product1.class);
      assertEquals(true, products.get(0).getOrderDetails().size() > 0);
      assertEquals(true, products.get(0).getOrderDetails().get(0).getOrder() != null);
      assertEquals(NUM_OF_PRODUCTS, products.size());
    }

    {
      msg("Query for products with a specific name");
      CDOQuery cdoQuery = transaction.createQuery("hql", "from Product1 where name=:name");
      cdoQuery.setParameter("name", "" + 1);
      addCacheParameter(cdoQuery);
      final List<Product1> products = cdoQuery.getResult(Product1.class);
      assertEquals(1, products.size());
    }

    clearCache(getRepository().getRevisionManager());

    {
      msg("Query for Customers");
      CDOQuery cdoQuery = transaction.createQuery("hql", "from Customer");
      addCacheParameter(cdoQuery);
      final List<Customer> customers = cdoQuery.getResult(Customer.class);
      assertEquals(true, customers.get(0).getSalesOrders().size() > 0);

      assertEquals(NUM_OF_CUSTOMERS, customers.size());
    }

    {
      msg("Query for products with VAT15");
      CDOQuery cdoQuery = transaction.createQuery("hql", "from Product1 where vat=:vat");
      addCacheParameter(cdoQuery);
      cdoQuery.setParameter("vat", VAT.VAT15);
      final List<Product1> products = cdoQuery.getResult(Product1.class);
      assertEquals(10, products.size());
      for (Product1 p : products)
      {
        assertEquals(p.getVat(), VAT.VAT15);
      }
    }

    transaction.commit();
    enableConsole();
  }

  public void testFunctions() throws Exception
  {
    msg("Opening session");
    CDOSession session = openSession();

    createTestSet(session);

    msg("Opening transaction for querying");
    CDOTransaction transaction = session.openTransaction();

    {
      msg("Count products");
      CDOQuery cdoQuery = transaction.createQuery("hql", "select count(*) from Product1");
      addCacheParameter(cdoQuery);
      final List<Long> counts = cdoQuery.getResult(Long.class);
      assertEquals(counts.size(), 1);
      assertEquals(counts.get(0), new Long(NUM_OF_PRODUCTS));
    }

    {
      msg("Orders with sum of order details");
      // result with arrays are tested below
      // CDOQuery cdoQuery = transaction.createQuery("hql",
      // "select so.id, sum(od.price) from SalesOrder so, OrderDetail od where od.order=so group by so.id");
      CDOQuery cdoQuery = transaction.createQuery("hql", "select sum(od.price) from SalesOrder so, OrderDetail od where od.order=so group by so.id");
      addCacheParameter(cdoQuery);
      final List<Double> results = cdoQuery.getResult(Double.class);
      assertEquals(NUM_OF_SALES_ORDERS * NUM_OF_CUSTOMERS, results.size());
    }

    transaction.commit();
    enableConsole();
  }

  public void testComplexQuerySalesOrderJoinCustomerProduct() throws Exception
  {
    msg("Opening session");
    CDOSession session = openSession();

    createTestSet(session);

    msg("Opening transaction for querying");
    CDOTransaction transaction = session.openTransaction();

    {
      msg("Query for customers");
      CDOQuery customerQuery = transaction.createQuery("hql", "from Customer order by name");
      addCacheParameter(customerQuery);
      final List<Customer> customers = customerQuery.getResult(Customer.class);
      assertEquals(NUM_OF_CUSTOMERS, customers.size());

      msg("Query for products");
      CDOQuery productQuery = transaction.createQuery("hql", "from Product1");
      addCacheParameter(productQuery);
      final List<Product1> products = productQuery.getResult(Product1.class);
      assertEquals(NUM_OF_PRODUCTS, products.size());

      msg("Query for all orders of a certain customer and with a certain product");
      int i = 0;
      for (Customer customer : customers)
      {
        final int customerIndex = Integer.parseInt(customer.getName());
        // check order by
        assertEquals(i++, customerIndex);

        final int productCounter = customerIndex * NUM_OF_PRODUCTS_CUSTOMER;
        for (Product1 product : products)
        {
          final int productIndex = Integer.parseInt(product.getName());
          // note the id is always used as the parameter
          // bug 282620
          final CDOQuery orderQuery = transaction.createQuery("hql",
              "select so from SalesOrder so, OrderDetail od where so.customer=:customer and od in elements(so.orderDetails) and od.product=:product");
          orderQuery.setParameter("customer", customer);
          orderQuery.setParameter("product", product);
          addCacheParameter(orderQuery);

          final boolean hasOrders = productCounter <= productIndex && productIndex < productCounter + NUM_OF_PRODUCTS_CUSTOMER;
          if (hasOrders)
          {
            final List<SalesOrder> sos = orderQuery.getResult(SalesOrder.class);
            assertEquals(NUM_OF_SALES_ORDERS, sos.size());
            for (SalesOrder so : sos)
            {
              assertEquals(customer, so.getCustomer());
              boolean found = false;
              for (OrderDetail od : so.getOrderDetails())
              {
                found |= od.getProduct() == product;
              }

              assertEquals(true, found);
            }
          }
          else
          {
            assertEquals(0, orderQuery.getResult(SalesOrder.class).size());
          }
        }
      }
    }

    transaction.commit();
    enableConsole();
  }

  public void testElementsClause() throws Exception
  {
    // tests
    // bug 282620

    msg("Opening session");
    CDOSession session = openSession();

    createTestSet(session);

    msg("Opening transaction for querying");
    CDOTransaction transaction = session.openTransaction();

    {
      msg("Query for customers");
      CDOQuery odQuery = transaction.createQuery("hql", "from OrderDetail");
      addCacheParameter(odQuery);
      final List<OrderDetail> orderDetails = odQuery.getResult(OrderDetail.class);
      for (OrderDetail orderDetail : orderDetails)
      {
        final CDOQuery orderQuery = transaction.createQuery("hql", "select so from SalesOrder so where :od in elements(so.orderDetails)");
        orderQuery.setParameter("od", orderDetail);
        final List<SalesOrder> sos = orderQuery.getResult(SalesOrder.class);
        assertEquals(1, sos.size());
        assertNotNull(sos.get(0).getCustomer().getName());
        assertEquals(orderDetail.getOrder(), sos.get(0));
      }
    }

    transaction.commit();
    enableConsole();
  }

  // Tests id handling
  // See: bug 283106
  public void testQueryWithID() throws Exception
  {
    msg("Opening session");
    CDOSession session = openSession();

    createTestSet(session);

    msg("Opening transaction for querying");
    CDOTransaction transaction = session.openTransaction();

    {
      msg("Query for customers");
      CDOQuery odQuery = transaction.createQuery("hql", "from Customer");
      addCacheParameter(odQuery);
      final List<Customer> customers = odQuery.getResult(Customer.class);
      for (Customer customer : customers)
      {
        final CDOQuery orderQuery = transaction.createQuery("hql", "select so from SalesOrder so where so.customer.id=:customerId");
        final CDOObject cdoObject = CDOUtil.getCDOObject(customer);
        final CDOID id = cdoObject.cdoID();
        orderQuery.setParameter("customerId", HibernateUtil.getInstance().getIdValue(id));
        final List<SalesOrder> sos = orderQuery.getResult(SalesOrder.class);
        assertEquals(NUM_OF_SALES_ORDERS, sos.size());
        for (SalesOrder so : sos)
        {
          assertEquals(customer, so.getCustomer());
        }
      }
    }

    transaction.commit();
    enableConsole();
  }

  public void testQueryObjectArray() throws Exception
  {
    msg("Opening session");
    CDOSession session = openSession();

    createTestSet(session);

    msg("Opening transaction for querying");
    CDOTransaction transaction = session.openTransaction();

    {
      msg("Query for order details");
      CDOQuery query = transaction.createQuery("hql", "select od, od.order, od.product.vat, od.price from OrderDetail as od where od.product.vat=:vat");
      query.setParameter("vat", VAT.VAT15);
      addCacheParameter(query);
      for (Object[] values : query.getResult(Object[].class))
      {
        assertInstanceOf(OrderDetail.class, values[0]);
        assertInstanceOf(SalesOrder.class, values[1]);
        assertInstanceOf(VAT.class, values[2]);
        assertEquals(VAT.VAT15, values[2]);
        assertInstanceOf(Float.class, values[3]);
        final SalesOrder order = (SalesOrder)values[1];
        assertEquals(true, order.getOrderDetails().contains(values[0]));
      }
    }

    transaction.commit();
    enableConsole();
  }

  public void testPaging() throws Exception
  {
    msg("Opening session");
    CDOSession session = openSession();

    createTestSet(session);

    msg("Opening transaction for querying");
    CDOTransaction transaction = session.openTransaction();

    {
      msg("Query for products in pages");
      int pageSize = 5;
      int numOfPages = NUM_OF_PRODUCTS / pageSize;
      final List<Product1> allProducts = new ArrayList<Product1>();
      for (int page = 0; page < numOfPages; page++)
      {
        CDOQuery productQuery = transaction.createQuery("hql", "from Product1");
        productQuery.setMaxResults(pageSize);
        productQuery.setParameter(IHibernateStore.FIRST_RESULT, page * pageSize);
        addCacheParameter(productQuery);
        final List<Product1> queriedProducts = productQuery.getResult(Product1.class);
        assertEquals(true, queriedProducts.size() <= pageSize);
        // a product should not have been read yet
        for (Product1 newProduct : queriedProducts)
        {
          assertEquals(true, !allProducts.contains(newProduct));
        }

        allProducts.addAll(queriedProducts);
      }

      assertEquals(NUM_OF_PRODUCTS, allProducts.size());
    }

    transaction.commit();
    enableConsole();
  }

  public void testIterator() throws Exception
  {
    msg("Opening session");
    CDOSession session = openSession();

    createTestSet(session);

    msg("Opening transaction for querying");
    CDOTransaction transaction = session.openTransaction();

    {
      msg("Query for products");
      CDOQuery productQuery = transaction.createQuery("hql", "from Product1");
      addCacheParameter(productQuery);
      final CloseableIterator<Product1> iterator = productQuery.getResultAsync(Product1.class);
      int counter = 0;
      while (iterator.hasNext())
      {
        final Product1 product = iterator.next();
        // meaningless but do something
        assertEquals(true, product != null);
        counter++;
        if (counter == NUM_OF_PRODUCTS / 2)
        {
          iterator.close();
        }
      }
    }

    transaction.commit();
    enableConsole();
  }

  private void createTestSet(CDOSession session)
  {
    disableConsole();
    msg("Opening transaction");
    CDOTransaction transaction = session.openTransaction();

    msg("Creating resource");
    CDOResource resource = transaction.createResource(getResourcePath("/test1"));

    fillResource(resource);

    try
    {
      msg("Committing");
      transaction.commit();
    }
    catch (CommitException ex)
    {
      throw WrappedException.wrap(ex);
    }

    enableConsole();
  }

  private void fillResource(CDOResource resource)
  {
    msg("Creating Testset");
    final List<Product1> products = new ArrayList<Product1>();
    for (int i = 0; i < NUM_OF_PRODUCTS; i++)
    {
      products.add(createProduct(i));
    }

    resource.getContents().addAll(products);

    int productCounter = 0;
    for (int i = 0; i < NUM_OF_CUSTOMERS; i++)
    {
      final Customer customer = getModel1Factory().createCustomer();
      customer.setCity("City " + i);
      customer.setName(i + "");
      customer.setStreet("Street " + i);
      resource.getContents().add(customer);

      final List<Product1> customerProducts = products.subList(productCounter, productCounter + NUM_OF_PRODUCTS_CUSTOMER);
      for (int k = 0; k < NUM_OF_SALES_ORDERS; k++)
      {
        resource.getContents().add(createSalesOrder(i * 10 + k, customer, customerProducts));
      }

      productCounter += NUM_OF_PRODUCTS_CUSTOMER;
    }

    final List<Supplier> suppliers = new ArrayList<Supplier>();
    for (int i = 0; i < NUM_OF_SUPPLIERS; i++)
    {
      suppliers.add(createSupplier(i));
    }

    resource.getContents().addAll(suppliers);
  }

  private Supplier createSupplier(int i)
  {
    Supplier supplier = getModel1Factory().createSupplier();
    supplier.setCity("City " + i);
    supplier.setName(i + "");
    supplier.setStreet("Street " + i);
    // supplier.setPreferred(false); // will be persisted with its default value
    return supplier;
  }

  private SalesOrder createSalesOrder(int num, Customer customer, List<Product1> products)
  {
    SalesOrder salesOrder = getModel1Factory().createSalesOrder();
    salesOrder.setCustomer(customer);
    salesOrder.setId(num);
    createOrderDetail(salesOrder, num, products);
    return salesOrder;
  }

  private List<OrderDetail> createOrderDetail(Order order, int index, List<Product1> products)
  {
    final List<OrderDetail> orderDetails = new ArrayList<OrderDetail>();
    int count = 0;
    for (Product1 product : products)
    {
      OrderDetail orderDetail = getModel1Factory().createOrderDetail();
      orderDetail.setOrder(order);
      orderDetail.setPrice(count++ * index * 1.1f);
      orderDetail.setProduct(product);
    }

    return orderDetails;
  }

  private Product1 createProduct(int index)
  {
    Product1 product = getModel1Factory().createProduct1();
    product.setDescription("Description " + index);
    product.setName("" + index);
    if (index < 10)
    {
      product.setVat(VAT.VAT15);
    }
    else
    {
      product.setVat(VAT.VAT7);
    }

    return product;
  }

  protected void addCacheParameter(CDOQuery query)
  {
    query.setParameter(IHibernateStore.CACHE_RESULTS, true);
  }
}

Back to the top