2 Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
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.
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.
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
18 package com.mysql.clusterj.jpatest;
20 import javax.persistence.Query;
22 import com.mysql.clusterj.jpatest.model.IdBase;
23 import com.mysql.clusterj.jpatest.model.LazyEmployee;
26 /** Test lazy loading support. Currently only remove and insert are tested.
30 drop table if exists t_basic;
31 create table t_basic (
33 name varchar(32), // lazy with load fetch group (name, age)
34 age int, // lazy with no load fetch group
38 create unique index idx_unique_hash_magic using hash on t_basic(magic);
39 create index idx_btree_age on t_basic(age);
43 public class LazyTest extends AbstractJPABaseTest {
45 private int NUMBER_OF_INSTANCES = 4;
47 /** Subclasses must override this method to provide the model class for the test */
48 protected Class<? extends IdBase> getModelClass() {
49 return LazyEmployee.class;
53 removeAll(LazyEmployee.class);
54 em.getTransaction().begin();
55 for (int i = 0; i < NUMBER_OF_INSTANCES ; ++i) {
56 LazyEmployee e = createLazyEmployee(i);
59 em.getTransaction().commit();
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();
67 int magic = e.getMagic();
68 // name and age are lazily loaded
72 // age and name are loaded separately because age has no load fetch group
76 // age and name are loaded together because name's load fetch group includes age
80 String result = new String("Lazy Employee " + id + " magic: " + magic + " age: " + age + " name: " + name);
81 // System.out.println(result);
83 em.getTransaction().commit();
86 private LazyEmployee createLazyEmployee(int i) {
87 LazyEmployee lazy = new LazyEmployee();
90 lazy.setName("LazyEmployee " + i);