]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
c54f371025ec56dd9456b873526938b8ab480205
[packages/trusty/mysql-wsrep-5.6.git] /
1 /*
2    Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
16 */
17
18 package com.mysql.clusterj.jpatest;
19
20 import javax.persistence.Query;
21
22 import com.mysql.clusterj.jpatest.model.IdBase;
23 import com.mysql.clusterj.jpatest.model.LazyEmployee;
24
25
26 /** Test lazy loading support. Currently only remove and insert are tested.
27  * 
28  * Schema
29  * 
30 drop table if exists t_basic;
31 create table t_basic (
32   id int not null,
33   name varchar(32), // lazy with load fetch group (name, age)
34   age int,          // lazy with no load fetch group
35   magic int not null,
36   primary key(id)) 
37   engine=ndbcluster;
38 create unique index idx_unique_hash_magic using hash on t_basic(magic);
39 create index idx_btree_age on t_basic(age);
40
41 */
42 @Ignore
43 public class LazyTest extends AbstractJPABaseTest {
44
45     private int NUMBER_OF_INSTANCES = 4;
46
47     /** Subclasses must override this method to provide the model class for the test */
48     protected Class<? extends IdBase> getModelClass() {
49         return LazyEmployee.class;
50     }
51
52     public void test() {
53         removeAll(LazyEmployee.class);
54         em.getTransaction().begin();
55         for (int i = 0; i < NUMBER_OF_INSTANCES ; ++i) {
56             LazyEmployee e = createLazyEmployee(i);
57             em.persist(e);
58         }
59         em.getTransaction().commit();
60         em.clear();
61         em.getTransaction().begin();
62         Query query = em.createQuery("select e from LazyEmployee e where e.id = :id");
63         for (int i = 0; i < NUMBER_OF_INSTANCES ; ++i) {
64             query.setParameter("id", i);
65             LazyEmployee e = (LazyEmployee)query.getSingleResult();
66             int id = e.getId();
67             int magic = e.getMagic();
68             // name and age are lazily loaded
69             final int age;
70             final String name;
71             if (0 == i%2) {
72                 // age and name are loaded separately because age has no load fetch group
73                 age = e.getAge();
74                 name = e.getName();
75             } else {
76                 // age and name are loaded together because name's load fetch group includes age
77                 name = e.getName();
78                 age = e.getAge();
79             }
80             String result = new String("Lazy Employee " + id + " magic: " + magic + " age: " + age + " name: " + name);
81 //            System.out.println(result);
82         }
83         em.getTransaction().commit();
84     }
85
86     private LazyEmployee createLazyEmployee(int i) {
87         LazyEmployee lazy = new LazyEmployee();
88         lazy.setId(i);
89         lazy.setAge(i);
90         lazy.setName("LazyEmployee " + i);
91         lazy.setMagic(i);
92         return lazy;
93     }
94
95 }