5097N/A/*
5097N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5097N/A *
5097N/A * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
5097N/A *
5097N/A * The contents of this file are subject to the terms of either the GNU
5097N/A * General Public License Version 2 only ("GPL") or the Common Development
5097N/A * and Distribution License("CDDL") (collectively, the "License"). You
5097N/A * may not use this file except in compliance with the License. You can
5097N/A * obtain a copy of the License at
5097N/A * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
5097N/A * or packager/legal/LICENSE.txt. See the License for the specific
5097N/A * language governing permissions and limitations under the License.
5097N/A *
5097N/A * When distributing the software, include this License Header Notice in each
5097N/A * file and include the License file at packager/legal/LICENSE.txt.
5097N/A *
5097N/A * GPL Classpath Exception:
5097N/A * Oracle designates this particular file as subject to the "Classpath"
5097N/A * exception as provided by Oracle in the GPL Version 2 section of the License
5680N/A * file that accompanied this code.
5097N/A *
5680N/A * Modifications:
5680N/A * If applicable, add the following below the License Header, with the fields
5680N/A * enclosed by brackets [] replaced by your own identifying information:
5097N/A * "Portions Copyright [year] [name of copyright owner]"
5097N/A *
5097N/A * Contributor(s):
5097N/A * If you wish your version of this file to be governed by only the CDDL or
5097N/A * only the GPL Version 2, indicate your decision by adding "[Contributor]
5097N/A * elects to include this software in this distribution under the [CDDL or GPL
5097N/A * Version 2] license." If you don't indicate a single choice of license, a
5097N/A * recipient has the option to distribute your version of this file under
5097N/A * either the CDDL, the GPL Version 2 or to extend the choice of license to
5097N/A * its licensees as provided above. However, if you add GPL Version 2 code
5097N/A * and therefore, elected the GPL Version 2 license, then the option applies
5097N/A * only if the new code is made subject to such option by the copyright
5097N/A * holder.
5680N/A */
5097N/A
5097N/Apackage myapp;
5097N/A
5097N/Aimport javax.persistence.*;
5097N/Aimport javax.transaction.*;
5097N/Aimport java.io.*;
5097N/A
5097N/Apublic class JpaTest {
5097N/A
5097N/A private EntityManagerFactory emf;
5097N/A private EntityManager em;
5097N/A private UserTransaction utx;
5097N/A PrintWriter out;
5680N/A private static Department deptRef[] = new Department[2];
5097N/A private static Employee empRef[] = new Employee[5];
5097N/A
5097N/A public JpaTest(){}
5097N/A
5097N/A public JpaTest(EntityManagerFactory emf, EntityManager em,
5097N/A UserTransaction utx, PrintWriter out){
5097N/A this.emf = emf;
5097N/A this.em = em;
5097N/A this.utx = utx;
5097N/A this.out = out;
5097N/A }
5097N/A
5097N/A protected boolean lazyLoadingInit(){
5097N/A boolean status = false;
5097N/A out.println("-----lazeLoadingInit()---------");
5097N/A try {
5097N/A deptRef[0] = new Department(1, "Engineering");
5097N/A deptRef[1] = new Department(2, "Marketing");
5097N/A utx.begin();
5097N/A em.joinTransaction();
5097N/A for (int i=0; i < 2; i++ ) {
5097N/A em.persist(deptRef[i]);
5097N/A }
5097N/A utx.commit();
5097N/A
5097N/A empRef[0] = new Employee(1, "Alan", "Frechette", deptRef[0]);
5097N/A empRef[1] = new Employee(2, "Arthur", "Wesley", deptRef[0]);
5097N/A empRef[2] = new Employee(3, "Abe", "White", deptRef[0]);
5097N/A empRef[3] = new Employee(4, "Paul", "Hinz", deptRef[1]);
5097N/A empRef[4] = new Employee(5, "Carla", "Calrson", deptRef[1]);
5097N/A utx.begin();
5097N/A em.joinTransaction();
5097N/A for (int i=0; i<5; i++ ) {
5097N/A em.persist(empRef[i]);
5097N/A }
5097N/A utx.commit();
5680N/A status = true;
5097N/A } catch(Exception ex){
5097N/A ex.printStackTrace();
5097N/A }
5097N/A out.println("-----status = " + status + "---------");
5097N/A return status;
5097N/A }
5097N/A
5097N/A public boolean lazyLoadingByFind(int employeeID) {
5097N/A
5097N/A boolean status=true;
5097N/A out.println("------------lazyLoadingAfterFind -----------");
5097N/A out.println("employeeID = "+ employeeID);
5097N/A Employee emp = em.find(Employee.class, employeeID);
5097N/A
5097N/A out.println("found: emp.id=" + emp.getId());
5097N/A
5097N/A try {
5097N/A // 1. get Department before loading
5097N/A Department deptBL = emp.getDepartmentNoWeaving();
5097N/A out.println("1. before loading: deptBL="+deptBL);
5097N/A String deptNameBL = null;
5097N/A if (deptBL != null) {
5097N/A deptNameBL = deptBL.getName();
5097N/A out.println("deptNameBL="+deptNameBL);
5097N/A }
5097N/A // assert deptBL == null;
5097N/A if (deptBL != null) {
5097N/A status = false;
5097N/A }
5097N/A
5097N/A // 2. loading
5097N/A String deptName = emp.getDepartment().getName();
5097N/A out.println("2. loading, deptName = " + deptName);
5097N/A
5097N/A // 3. get Department after loading
5097N/A Department deptAL = emp.getDepartmentNoWeaving();
5097N/A out.println("3. after loading: deptAL="+deptAL);
5097N/A String deptNameAL = deptAL.getName();
5097N/A System.out.println("deptNameAL="+deptNameAL);
5097N/A // assert deptAL != null
5097N/A // assert deptAL.getName == deptName;
5097N/A if (deptAL == null || deptNameAL != deptName) {
5097N/A status = false;
5097N/A }
5097N/A } catch(Exception ex){
5097N/A status = false;
5097N/A ex.printStackTrace();
5097N/A }
5097N/A
5097N/A out.println("-----status = " + status + "---------");
5097N/A return status;
5097N/A }
5097N/A
5097N/A public boolean lazyLoadingByQuery(String fName) {
5097N/A
5097N/A boolean status=true;
5097N/A out.println("------------lazyLoadingByQuery -----------");
5097N/A out.println("fName = "+ fName);
5097N/A Query query = em.createQuery(
5097N/A "SELECT e FROM Employee e WHERE e.firstName like :firstName")
5097N/A .setParameter("firstName", fName);;
5097N/A Employee emp = (Employee) query.getSingleResult();
5097N/A
5097N/A out.println("queried: emp.firstName=" + emp.getFirstName());
5097N/A
5097N/A try {
5097N/A // 1. get Department before loading
Department deptBL = emp.getDepartmentNoWeaving();
out.println("1. before loading: deptBL="+deptBL);
String deptNameBL = null;
if (deptBL != null) {
deptNameBL = deptBL.getName();
out.println("deptNameBL="+deptNameBL);
}
// assert deptBL == null;
if (deptBL != null) {
status = false;
}
// 2. loading
String deptName = emp.getDepartment().getName();
System.out.println("2. loading, deptName = " + deptName);
// 3. get Department after loading
Department deptAL = emp.getDepartmentNoWeaving();
out.println("3. after loading: deptAL="+deptAL);
String deptNameAL = deptAL.getName();
out.println("deptNameAL="+deptNameAL);
// assert deptAL != null
// assert deptAL.getName == deptName;
if (deptAL == null || deptNameAL != deptName) {
status = false;
}
} catch(Exception ex){
status = false;
ex.printStackTrace();
}
out.println("-----status = " + status + "---------");
return status;
}
}