OPENJPA-370. Reverting this change for the 1.0.1 release as well (since that release was cut before we made this decision to back out the changes).
git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/1.0.1@591364 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DelegatingFetchConfiguration.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DelegatingFetchConfiguration.java
index d94604e..dcbe31e 100644
--- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DelegatingFetchConfiguration.java
+++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DelegatingFetchConfiguration.java
@@ -18,7 +18,6 @@
*/
package org.apache.openjpa.kernel;
-import java.util.BitSet;
import java.util.Collection;
import java.util.Set;
@@ -447,15 +446,7 @@
throw translate(re);
}
}
-
- public BitSet requiresFetch(Set fgs, FieldMetaData[] fmds ){
- try {
- return _fetch.requiresFetch(fgs, fmds);
- } catch (RuntimeException re) {
- throw translate(re);
- }
- }
-
+
public boolean requiresLoad() {
try {
return _fetch.requiresLoad();
diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/FetchConfiguration.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/FetchConfiguration.java
index 40c00ca..1fdfce4 100644
--- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/FetchConfiguration.java
+++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/FetchConfiguration.java
@@ -19,7 +19,6 @@
package org.apache.openjpa.kernel;
import java.io.Serializable;
-import java.util.BitSet;
import java.util.Collection;
import java.util.Set;
@@ -347,18 +346,6 @@
public int requiresFetch(FieldMetaData fm);
/**
- * Affirms if the given fields require to be fetched in the context of
- * the given fetch group set. Returns a BitSet that contains one of
- * {@link #FETCH_NONE}, {@link #FETCH_LOAD}, {@link FETCH_REF} for each
- * field.
- *
- * @param fgs fetch group set
- * @param fmds array of fields to be examined
- * @return BitSet that indicates whether fetches are required or not
- */
- public BitSet requiresFetch(Set fgs, FieldMetaData[] fmds );
-
- /**
* Return false if we know that the object being fetched with this
* configuration does not require a load, because this configuration came
* from a traversal of a {@link #FETCH_REF} field.
diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/FetchConfigurationImpl.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/FetchConfigurationImpl.java
index 17e0cc5..0975f87 100644
--- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/FetchConfigurationImpl.java
+++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/FetchConfigurationImpl.java
@@ -21,7 +21,6 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.BitSet;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -93,7 +92,6 @@
private boolean _load = true;
private int _availableRecursion;
private int _availableDepth;
- private Map _lfgFields = null;
public FetchConfigurationImpl() {
this(null);
@@ -512,10 +510,6 @@
if (!includes(fm))
return FETCH_NONE;
- return indirectFetch(fm);
- }
-
- private int indirectFetch(FieldMetaData fm){
Class type = getRelationType(fm);
if (type == null)
return FETCH_LOAD;
@@ -535,15 +529,6 @@
return FETCH_REF;
return FETCH_LOAD;
}
-
- public BitSet requiresFetch(Set fgs, FieldMetaData[] fmds) {
- BitSet fields = new BitSet(fgs.size());
- Iterator itr = fgs.iterator();
- while (itr.hasNext()) {
- fields = includes((FieldMetaData) itr.next(), fmds, fields);
- }
- return fields;
- }
public boolean requiresLoad() {
return _load;
@@ -582,73 +567,16 @@
|| hasField(fmd.getFullName(false)))
return true;
String[] fgs = fmd.getCustomFetchGroups();
- for (int i = 0; i < fgs.length; i++){
+ for (int i = 0; i < fgs.length; i++)
if (hasFetchGroup(fgs[i]))
return true;
- }
return false;
}
- private BitSet includes(FieldMetaData fmd, FieldMetaData[] fmds,
- BitSet fields) {
- if ((fmd.isInDefaultFetchGroup() && hasFetchGroup(FetchGroup.NAME_DEFAULT))
- || hasFetchGroup(FetchGroup.NAME_ALL)
- || hasField(fmd.getFullName(false))) {
- if (indirectFetch(fmd) != FETCH_NONE)
- fields.set(fmd.getIndex());
- return fields;
- }
- // now we need to see if this field associates with
- // any fetch groups
- String[] fgs = fmd.getCustomFetchGroups();
- for (int i = 0; i < fgs.length; i++) {
- if (hasFetchGroup(fgs[i])) {
- if (indirectFetch(fmd) != FETCH_NONE)
- fields.set(fmd.getIndex());
- // check whether this field has a loadFetchGroup
- // if it has a LoadFetchGroup, then we need to get
- // all the fields that associate with this LoadFetchGroup
- String fg = fmd.getLoadFetchGroup();
- if (fg != null) {
- BitSet fldIndex = getLoadFetchGroupFields(fg, fmds);
- // merge the loadFetchGroup fields to the retuned fields.
- if (fldIndex != null && !fldIndex.isEmpty()) {
- for (int j = 0; j < fldIndex.length(); j++)
- if (fldIndex.get(j))
- fields.set(j);
- }
- }
- }
- }
- return fields;
- }
-
/**
- * Search the fields for this loadFetchGroup. It basically searches from
- * each field and check whether this field has the same fetchgroup. If it
- * has then this field is required to load.
- */
- private BitSet getLoadFetchGroupFields(String fg, FieldMetaData[] fmds) {
- BitSet rtnField = null;
- if (_lfgFields != null && _lfgFields.containsKey(fg))
- return (BitSet)_lfgFields.get(fg);
- for (int i = 0; i < fmds.length; i++) {
- if (fmds[i].isInFetchGroup(fg)){
- if (rtnField == null)
- rtnField = new BitSet(fmds.length);
- rtnField.set(i);
- }
- }
- if (_lfgFields == null)
- _lfgFields = new HashMap(fmds.length);
- _lfgFields.put(fg,rtnField);
- return rtnField;
- }
-
- /**
- * Return the available recursion depth via the given field for the
+ * Return the available recursion depth via the given field for the
* given type.
- *
+ *
* @param traverse whether we're traversing the field
*/
private int getAvailableRecursionDepth(FieldMetaData fm, Class type,
diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java
index 5018d1c..6d7486b 100644
--- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java
+++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java
@@ -427,22 +427,18 @@
BitSet fields = null;
FieldMetaData[] fmds = _meta.getFields();
- Set fgs = null;
boolean load;
-
for (int i = 0; i < fmds.length; i++) {
- if (exclude != null && exclude.get(i))
- continue;
+ if (_loaded.get(i) || (exclude != null && exclude.get(i)))
+ continue;
switch (mode) {
case LOAD_SERIALIZE:
load = !fmds[i].isTransient();
break;
- case LOAD_FGS:
- load = false;
- if (fgs == null)
- fgs = new HashSet(fmds.length);
- fgs.add(fmds[i]);
+ case LOAD_FGS:
+ load = fetch == null || fetch.requiresFetch(fmds[i])
+ != FetchConfiguration.FETCH_NONE;
break;
default: // LOAD_ALL
load = true;
@@ -453,22 +449,6 @@
fields = new BitSet(fmds.length);
fields.set(i);
}
- // post process for the fetchGroup: if there is a
- // fetchgroup field, then go to the FetchConfiguration
- // to get the required fetch fields.
- if (fgs != null && fetch != null){
- if (fields == null)
- fields = new BitSet(fmds.length);
- BitSet fgFields = fetch.requiresFetch(fgs, fmds);
- // merge the fetchgroup required fields to the original
- // fields only the fields are not already loaded and
- // are not in the original fields.
- for (int j = 0; j < fgFields.length(); j++) {
- if (fgFields.get(j) && !fields.get(j) && !_loaded.get(j))
- fields.set(j);
- }
- }
-
}
return fields;
}
diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/fetchgroups/TestFetchGroup.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/fetchgroups/TestFetchGroup.java
deleted file mode 100644
index 2f1e9be..0000000
--- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/fetchgroups/TestFetchGroup.java
+++ /dev/null
@@ -1,564 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.openjpa.persistence.fetchgroups;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-
-import javax.persistence.EntityManager;
-import javax.persistence.Query;
-
-import org.apache.openjpa.persistence.OpenJPAEntityManager;
-import org.apache.openjpa.persistence.OpenJPAPersistence;
-import org.apache.openjpa.persistence.OpenJPAQuery;
-import org.apache.openjpa.persistence.test.SingleEMTestCase;
-
-public class TestFetchGroup extends SingleEMTestCase {
- public void setUp() {
- setUp(FGEmployee.class, FGDepartment.class, FGManager.class,
- FGAddress.class);
- EntityManager em = emf.createEntityManager();
- OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
-
- // Populate database as denoted in Entity Data
- boolean errors = initializeDatabase(oem);
- assertFalse(errors);
- }
-
- // Test no fetch group is added.
- public void testFetchGroup001() {
- // System.out.println("***********************************************");
- // System.out.println("******** 001 ==> test no fetch group is added
- // thru API");
- EntityManager em = emf.createEntityManager();
- OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
-
- FGEmployee emp = findEmployee(oem, 1, false, null);
-
- // Examine Employee(id=1).rating, data should be available
- // System.out.println("Assert Employee(id=1).rating should be null.
- // Result ==>");
- assertNull(emp.getRating());
- // Examine Employee(id=1).description, data should NOT be available
- // System.out.println("Assert Employee(id=1).description is null. Result
- // ==>");
- assertNull(emp.getDescription());
- // Examine Employee(id=1).address, data should not be available
- // System.out.println("Assert Employee(id=1).address is null");
- assertNull(emp.getAddress());
- // Examine Employee(id=1).dept, data should NoTbe available
- // System.out.println("Assert Employee(id=1).dept is null ");
- assertNull(emp.getDept());
- // Examine Employee(id=1).manager, data should NOT be available.
- // System.out.println("Assert Employee(id=1).manager is null ");
- assertNull(emp.getManager());
-
- }
-
- // Test no fetch group is added and restFetchGroup is called.
- public void testFetchGroup002() {
- // System.out.println("***********************************************");
- // System.out.println("*****************002 ==> test no fetch group is
- // added thru API and ");
- // System.out.println(" resetFetchGroup is called.");
- EntityManager em = emf.createEntityManager();
- OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
-
- FGEmployee emp = findEmployee(oem, 1, true, null);
- // Examine Employee(id=1).rating, data should not be available
- // System.out.println("Assert Employee(id=1).rating is null.");
- assertNull(emp.getRating());
- // Examine Employee(id=1).description, data should NOT be available
- // System.out.println("Assert Employee(id=1).description is null. Result
- // ==>");
- assertNull(emp.getDescription());
- // Examine Employee(id=1).address, data should not be available
- // System.out.println("Assert Employee(id=1).address is null ");
- assertNull(emp.getAddress());
- // Examine Employee(id=1).dept, data should NoT be available
- // System.out.println("Assert Employee(id=1).dept is null ");
- assertNull(emp.getDept());
- // Examine Employee(id=1).manager, data should NOT be available.
- // System.out.println("Assert Employee(id=1).manager is null ");
- assertNull(emp.getManager());
-
- }
-
- // Test Rating fetch group is added and restFetchGroup is called.
- public void testFetchGroup003() {
- // System.out.println("***********************************************");
- // System.out.println("****************003 ==> test RatingFetchGroup is
- // added thru API and ");
- // System.out.println(" resetFetchGroup is called.");
- EntityManager em = emf.createEntityManager();
- OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
-
- String[] arr = { "RatingFetchGroup" };
- FGEmployee emp = findEmployee(oem, 1, true, arr);
- // Examine Employee(id=1).rating, data should be available
- // System.out.println("Assert Employee(id=1).rating is not null. Result
- // ==> ");
- assertNotNull(emp.getRating());
- // Examine Employee(id=1).description, data should NOT be available
- // System.out.println("Assert Employee(id=1).description is null. Result
- // ==>");
- assertNull(emp.getDescription());
- // Examine Employee(id=1).address, data should be available
- // System.out.println("Assert Employee(id=1).address is not null because
- // of @LoadFetchGroup on Rating ==> ");
- assertNotNull(emp.getAddress());
- // Examine Employee(id=1).dept, data should NoTbe available
- // System.out.println("Assert Employee(id=1).dept is null = ");
- assertNull(emp.getDept());
- // Examine Employee(id=1).manager, data should NOT be available.
- // System.out.println("Assert Employee(id=1).manager is null =");
- assertNull(emp.getManager());
- // em.getTransaction().commit();
-
- }
-
- // Test Address and Rating fetch groups are added and restFetchGroup is
- // called.
- public void testFetchGroup004() {
- // System.out.println("***********************************************");
- // System.out.println("***************004 ==> test RatingFetchGroup and
- // AddressFetchGroup" +
- // "are added thru API and " +
- // "resetFetchGroup is called.");
- EntityManager em = emf.createEntityManager();
- OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
-
- String[] arr = { "RatingFetchGroup", "AddressFetchGroup" };
- FGEmployee emp = findEmployee(oem, 1, true, arr);
- // Examine Employee(id=1).rating, data should be available
- // System.out.println("Assert Employee(id=1).rating is not null. Result
- // ==> ");
- assertNotNull(emp.getRating());
- // Examine Employee(id=1).description, data should NOT be available
- // System.out.println("Assert Employee(id=1).description is null. Result
- // ==>");
- assertNull(emp.getDescription());
- // Examine Employee(id=1).address, data should be available
- // System.out.println("Assert Employee(id=1).address is not null ==> ");
- assertNotNull(emp.getAddress());
- // Examine Employee(id=1).dept, data should NoTbe available
- // System.out.println("Assert Employee(id=1).dept is null = ");
- assertNull(emp.getDept());
- // Examine Employee(id=1).manager, data should NOT be available.
- // System.out.println("Assert Employee(id=1).manager is null =");
- assertNull(emp.getManager());
-
- }
-
- // Test aggregateEmployeeFetchGroup2 only
- public void testFetchGroup005() {
- // System.out.println("***********************************************");
- // System.out.println("***************005 ==> test
- // aggregateEmployeeFetchGroup2 only");
- EntityManager em = emf.createEntityManager();
- OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
-
- String[] arr = { "AggregateEmployeeFetchGroup2" };
- FGEmployee emp = findEmployee(oem, 1, true, arr);
- // Examine Employee(id=1).address, data should be available
-
- // System.out.println("Assert Employee(id=1).address is not null ");
- assertNotNull(emp.getAddress());
- // Examine Employee(id=1).dept, data should NoTbe available
- // System.out.println("Assert Employee(id=1).dept is not null = ");
- assertNotNull(emp.getDept());
- // Examine Employee(id=1).manager, data should NOT be available.
- // System.out.println("Assert Employee(id=1).manager is not null =");
- assertNotNull(emp.getManager());
-
- }
-
- public void testFetchGroup006() {
- // System.out.println("***********************************************");
- // System.out.println("************006 ==> test
- // aggregateEmployeeFetchGroup1 and "+
- // " aggregateEmployeeFetchGroup2 - expect address, dept and manager are
- // not null");
- EntityManager em = emf.createEntityManager();
- OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
- // em.getTransaction().begin();
-
- String[] arr = { "AggregateEmployeeFetchGroup1",
- "AggregateEmployeeFetchGroup2" };
- FGEmployee emp = findEmployee(oem, 1, true, arr);
- // Examine Employee(id=1).address, data should be available
- // System.out.println("Assert Employee(id=1).address is not null ");
- assertNotNull(emp.getAddress());
- // Examine Employee(id=1).dept, data should NoTbe available
- // System.out.println("Assert Employee(id=1).dept is not null ");
- assertNotNull(emp.getDept());
- // Examine Employee(id=1).manager, data should NOT be available.
- // FGManager mgr = emp.getManager();
- // System.out.println("assert manager is not null");
- assertNotNull(emp.getManager());
- assertNotNull(emp.getManager().getId());
- assertNotNull(emp.getManager().getFirstName());
-
- // Verify that Manager(id=101).manager is not available, as the
- // recursion depth should have retrieved only the Employee and its
- // manager.
- // System.out.println("Verify that Manager(id=101).manager is no
- // available, as the recursion depth should have retrieved only the
- // Employee and its manager.");
- // System.out.println("Assert Employee(id=1).manager.manager == null
- // ==>"+ mgrMgr);
- assertNull(emp.getManager().getManager());
-
- // System.out.println("Verify that Manager(id=201).manager is not
- // available, as the recursion depth should have retrieved only the
- // Employee and its manager.");
- // assertNull(emp.getManager().getManager().getManager());
-
- }
-
- public void testFetchGroup007() {
- // System.out.println("***********************************************");
- // System.out.println("***********007 ==> test one fetch group attribute
- // is associated"+
- // "multiple fetch groups");
- EntityManager em = emf.createEntityManager();
- OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
-
- String[] arr = { "ManagerFetchGroup1A" };
- FGEmployee emp = findEmployee(oem, 1, true, arr);
- // Examine Employee(id=1).address, data should be available
- // FGAddress addr = emp.getAddress();
- // System.out.println("Assert Employee(id=1).address is null ");
- assertNull(emp.getAddress());
- // Examine Employee(id=1).dept, data should NoTbe available
- // System.out.println("Assert Employee(id=1).dept is null");
- assertNull(emp.getDept());
- // Examine Employee(id=1).manager, data should NOT be available.
- // FGManager mgr = emp.getManager();
-
- // System.out.println("Assert manager is not null");
- assertNotNull(emp.getManager());
- assertNotNull(emp.getManager().getId());
- assertNotNull(emp.getManager().getFirstName());
-
- // Verify that Manager(id=101).manager is not available, as the
- // recursion depth should have retrieved only the Employee and its
- // manager.
- // System.out.println("Verify that Manager(id=101).manager is not
- // available, as the recursion depth should have retrieved only the
- // Employee and its manager.");
- assertNull(emp.getManager().getManager());
-
- // System.out.println("Verify that Manager(id=201).manager is not
- // available, as the recursion depth should have retrieved only the
- // Employee and its manager.");
- // assertNull(emp.getManager().getManager().getManager());
-
- }
-
- public void testFetchGroup008() {
- // System.out.println("***********************************************");
- // System.out.println("***********007 ==> test one fetch group attribute
- // is associated"+
- // "multiple fetch groups");
- EntityManager em = emf.createEntityManager();
- OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
- OpenJPAEntityManager oem1 = OpenJPAPersistence.cast(em);
- Query q = oem1.createQuery("SELECT e FROM FGEmployee e WHERE e.id = 1");
- OpenJPAQuery oq = (OpenJPAQuery) q;
- oem1.clear();
-
- // use the default, address and description should be null
- FGEmployee emp = findEmployeeForQuery(oem, oq, 1, true, null, null);
- oem1.clear();
- assertNull(emp.getAddress());
- assertNull(emp.getDescription());
- assertNull(emp.getManager());
-
- // add fetch fields to the fetch plan - address and description should
- // not be null
- String[] str = {
- "org.apache.openjpa.persistence.fetchgroups.FGEmployee.description",
- "org.apache.openjpa.persistence.fetchgroups.FGEmployee.address" };
- FGEmployee emp2 = findEmployeeForQuery(oem, oq, 1, true, str, null);
- oem1.clear();
- assertNotNull(emp2.getAddress());
- assertNotNull(emp2.getDescription());
- assertNull(emp2.getManager());
-
- // remove fetch fields again - address and description should be null
- FGEmployee emp3 = findEmployeeForQuery(oem, oq, 1, false, null, str);
- oem1.clear();
- assertNull(emp3.getAddress());
- assertNull(emp3.getDescription());
- assertNull(emp3.getManager());
- }
-
- private FGEmployee findEmployee(OpenJPAEntityManager oem, Object id,
- boolean reset, String[] fetchGroups) {
- oem.getTransaction().begin();
- // System.out.println("findEmployoee starts and check the fetchGroup
- // info:");
- // int sz = oem.getFetchPlan().getFetchGroups().size();
- // String arr =
- // Arrays.toString(oem.getFetchPlan().getFetchGroups().toArray());
- // System.out.println("fetchGroup = "+arr+ " and fetch Group size
- // ="+sz);
- // reset fetchGroup if necessary:
- if (reset) {
- oem.getFetchPlan().resetFetchGroups();
- // assertEquals(1, oem.getFetchPlan().getFetchGroups().size());
- // arr =
- // Arrays.toString(oem.getFetchPlan().getFetchGroups().toArray());
- // System.out.println("after resetFetchGroup, fetchGroup="+arr);
- // assertEquals("[default]",arr);
- }
- if (fetchGroups != null) {
- // System.out.println("input fetchGroup = "+fetchGroups);
- for (String fg : fetchGroups)
- oem.getFetchPlan().addFetchGroup(fg);
- // arr =
- // Arrays.toString(oem.getFetchPlan().getFetchGroups().toArray());
- // System.out.println("after addFetchGroup, fetchGroups = "+arr);
- }
- // System.out.println("Finding Employee(id=1)...");
- FGEmployee emp = oem.find(FGEmployee.class, id);
- // System.out.println("Employee found ="+emp);
- oem.getTransaction().commit();
- // oem.clear();
- oem.close();
- return emp;
-
- }
-
- private static FGEmployee findEmployeeForQuery(OpenJPAEntityManager oem,
- OpenJPAQuery oq, Object id, boolean reset, String[] fetchGroups,
- String[] removes) {
- oem.getTransaction().begin();
- // reset fetchGroup if necessary:
- if (reset) {
- oem.getFetchPlan().resetFetchGroups();
- oq.getFetchPlan().resetFetchGroups();
- }
- if (fetchGroups != null) {
- for (String fg : fetchGroups)
- oq.getFetchPlan().addField(fg);
- // arr =
- // Arrays.toString(oq.getFetchPlan().getFetchGroups().toArray());
- // arr = Arrays.toString(oq.getFetchPlan().getFields().toArray());
- // System.out.println("after addFetchfields, fetch fields = "+arr);
- }
- if (removes != null) {
- oq.getFetchPlan().removeFields(removes);
- // arr = Arrays.toString(oq.getFetchPlan().getFields().toArray());
- // System.out.println("after removeFetchGroup, fetch fields =
- // "+arr);
- }
- // System.out.println("Finding Employee(id=1)...");
- FGEmployee emp = (FGEmployee) oq.getSingleResult();
- oem.getTransaction().commit();
- oem.clear();
- // oem.close();
- return emp;
-
- }
-
- private static void cleanDatabase(EntityManager em) {
- // Clean out the database
- em.clear();
-
- String entityNames[] = { "FGEmployee", "FGAddress", "FGDepartment" };
-
- // System.out.println("Cleaning database.");
- try {
- // System.out.println("Starting transaction...");
- em.getTransaction().begin();
- // if (persistenceContextType == PERSISTENCECONTEXTTYPE_APPMGD)
- // em.joinTransaction();
-
- for (int index = 0; index < entityNames.length; index++) {
- String query = "SELECT a FROM " + entityNames[index] + " a";
- List entityAList = em.createQuery(query).getResultList();
-
- // Nothing returned, go to the next entity
- if (entityAList.size() == 0)
- continue;
-
- // System.out.println("Removing " + entityNames[index] + " data
- // from the database...");
-
- Iterator i = entityAList.iterator();
- while (i.hasNext()) {
- Object entity = i.next();
- // System.out.println("Removing entity " + entity.toString()
- // + " ...");
- em.remove(entity);
- }
- }
-
- // System.out.println("Committing transaction...");
- em.getTransaction().commit();
- } catch (Throwable t) {
- System.out.println("Caught exception during db cleanup" + t);
- } finally {
- try {
- if (em.getTransaction().isActive())
- em.getTransaction().rollback();
- } catch (Throwable t) {
- System.out
- .println("Caught exception transaction rollback in db cleanup failure recovery"
- + t);
- // throw t;
- }
- }
-
- // System.out.println("Done cleaning database.");
- }
-
- private static boolean initializeDatabase(EntityManager em) {
- // Clean the database first
- cleanDatabase(em);
-
- // System.out.println("Creating entities...");
- boolean errors = false;
- try {
- // Persist all entities to the database
- // System.out.println("Starting transaction...");
- em.getTransaction().begin();
- // if (persistenceContextType == PERSISTENCECONTEXTTYPE_APPMGD)
- // em.joinTransaction();
-
- // Addreesses
- FGAddress[] addresses = new FGAddress[11];
- addresses[0] = new FGAddress(1, "1010 29th Ave NW", "Rochester",
- "MN", 55901);
- addresses[1] = new FGAddress(2, "2020 29th Ave NW", "Rochester",
- "MN", 55901);
- addresses[2] = new FGAddress(3, "5000 Pilot Knob", "Rochester",
- "MN", 55902);
- addresses[3] = new FGAddress(4, "8192 Galaxie Avenue",
- "Apple Valley", "MN", 55209);
- addresses[4] = new FGAddress(5, "9100 Knight Drive", "Fargo", "ND",
- 58202);
- addresses[5] = new FGAddress(6, "312 Sioux Lane", "Bismarck", "ND",
- 58102);
- addresses[6] = new FGAddress(7, "5124 Grinch Circle", "Mason City",
- "IA", 24241);
- addresses[7] = new FGAddress(8, "1201 Citrus Lane", "Raleigh",
- "NC", 12345);
- addresses[8] = new FGAddress(9, "1501 Lemon Lane", "Raleigh", "NC",
- 12345);
- addresses[9] = new FGAddress(10, "2903 Orange Drive", "Raleigh",
- "NC", 12345);
- addresses[10] = new FGAddress(11, "1511 Kiwi Circle", "Raleigh",
- "NC", 12345);
-
- // System.out.println("Persisting Address entities...");
- for (int index = 0; index < addresses.length; index++) {
- em.persist(addresses[index]);
- }
-
- // Departments
- FGDepartment[] departments = new FGDepartment[7];
- for (int index = 0; index < 7; index++) {
- departments[index] = new FGDepartment(index + 1, "Department "
- + (index + 1));
- }
-
- // System.out.println("Persisting Department entities...");
- for (int index = 0; index < departments.length; index++) {
- em.persist(departments[index]);
- }
-
- // Managers
- Collection<FGEmployee> emptyCollection = new ArrayList<FGEmployee>();
- FGManager[] managers = new FGManager[6];
- managers[0] = new FGManager(301, "Elric", "Scotch",
- "Description MMM1", departments[6], addresses[10],
- (FGManager) null, "Good", emptyCollection, "MData301");
- managers[1] = new FGManager(202, "Cedric", "Clue",
- "Description MM2", departments[5], addresses[9],
- managers[0], "Good", emptyCollection, "MData202");
- managers[2] = new FGManager(201, "Bill", "Editor",
- "Description MM1", departments[5], addresses[8],
- managers[0], "Good", emptyCollection, "MData201");
- managers[3] = new FGManager(103, "Sue", "Taylor", "Description M3",
- departments[4], addresses[8], managers[1], "Good",
- emptyCollection, "MData103");
- managers[4] = new FGManager(102, "Alfred", "Newmann",
- "Description M2", departments[3], addresses[7],
- managers[2], "Good", emptyCollection, "MData102");
- managers[5] = new FGManager(101, "Jim", "Mitternacht",
- "Description M1", departments[3], addresses[6],
- managers[2], "Good", emptyCollection, "MData101");
-
- // System.out.println("Persisting Manager entities...");
- for (int index = 0; index < managers.length; index++) {
- em.persist(managers[index]);
- }
-
- // Employees
- FGEmployee[] employees = new FGEmployee[8];
- employees[0] = new FGEmployee(1, "John", "Doe", "Description 1",
- departments[0], addresses[0], managers[5], "Good");
- employees[1] = new FGEmployee(2, "Jane", "Doe", "Description 2",
- departments[0], addresses[0], managers[5], "Good");
- employees[2] = new FGEmployee(3, "Steve", "Martin",
- "Description 3", departments[0], addresses[1], managers[5],
- "Good");
- employees[3] = new FGEmployee(4, "Mark", "Scrabble",
- "Description 4", departments[1], addresses[2], managers[4],
- "Good");
- employees[4] = new FGEmployee(5, "Stacy", "Life", "Description 5",
- departments[1], addresses[3], managers[4], "Good");
- employees[5] = new FGEmployee(6, "Alx", "Indigo", "Description 6",
- departments[2], addresses[5], managers[3], "Good");
- employees[6] = new FGEmployee(7, "John", "Einstein",
- "Description 7", departments[2], addresses[5], managers[3],
- "Good");
- employees[7] = new FGEmployee(8, "Max", "Headroom",
- "Description 7", departments[5], addresses[3], managers[2],
- "Good");
-
- // System.out.println("Persisting Employee entities...");
- for (int index = 0; index < employees.length; index++) {
- em.persist(employees[index]);
- }
-
- // System.out.println("Committing transaction...");
- em.getTransaction().commit();
- } catch (Throwable t) {
- // System.out.println("Caught exception during db populating"+ t);
- errors = true;
- } finally {
- try {
- if (em.getTransaction().isActive())
- em.getTransaction().rollback();
- } catch (Throwable t) {
- // System.out.println("Caught exception transaction rollback in
- // db population failure recovery"+ t);
- }
- }
-
- return errors;
- }
-
-} // end of TestFetchGroup