Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/TestSpec10_1_26.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/TestSpec10_1_26.java?rev=750517&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/TestSpec10_1_26.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/TestSpec10_1_26.java Thu Mar 5 17:30:20 2009 @@ -0,0 +1,349 @@ +/* + * 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.jdbc.maps.spec_10_1_26_ex0; + +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import junit.framework.*; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Query; + +import org.apache.openjpa.lib.jdbc.AbstractJDBCListener; +import org.apache.openjpa.lib.jdbc.JDBCEvent; +import org.apache.openjpa.lib.jdbc.JDBCListener; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +public class TestSpec10_1_26 extends SingleEMFTestCase { + + public int numDepartments = 2; + public int numEmployeesPerDept = 2; + public List<String> namedQueries = new ArrayList<String>(); + + public int deptId = 1; + public int empId = 1; + + protected List<String> sql = new ArrayList<String>(); + protected int sqlCount; + + public void setUp() { + super.setUp(DROP_TABLES, + Department1.class, + Department2.class, + Department3.class, + Employee1.class, + Employee2.class, + Employee3.class, + EmployeeName3.class, + EmployeePK2.class, + "openjpa.jdbc.JDBCListeners", + new JDBCListener[] { + this.new Listener() + }); + createObj(); + } + + public void testQueryQualifiedId() throws Exception { + EntityManager em = emf.createEntityManager(); + String query = "select KEY(e) from Department1 d, " + + " in (d.empMap) e"; + List rs = em.createQuery(query).getResultList(); + Integer d = (Integer) rs.get(0); + String query2 = "select KEY(e) from Department2 d, " + + " in (d.empMap) e"; + List rs2 = em.createQuery(query2).getResultList(); + EmployeePK2 d2 = (EmployeePK2) rs2.get(0); + String query3 = "select KEY(e) from Department3 d, " + + " in (d.emps) e"; + List rs3 = em.createQuery(query3).getResultList(); + EmployeeName3 d3 = (EmployeeName3) rs3.get(0); + em.close(); + } + + public void testQueryObject() { + queryObj(); + } + + public List<String> getSql() { + return sql; + } + + public int getSqlCount() { + return sqlCount; + } + + + public void createObj() { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + for (int i = 0; i < numDepartments; i++) + createDepartment1(em, deptId++); + + for (int i = 0; i < numDepartments; i++) + createDepartment2(em, deptId++); + + for (int i = 0; i < numDepartments; i++) + createDepartment3(em, deptId++); + + tran.begin(); + em.flush(); + tran.commit(); + em.close(); + } + + public void createDepartment1(EntityManager em, int id) { + Department1 d = new Department1(); + d.setDeptId(id); + Map empMap = new HashMap(); + for (int i = 0; i < numEmployeesPerDept; i++) { + Employee1 e = createEmployee1(em, empId++); + //d.addEmployee1(e); + empMap.put(e.getEmpId(), e); + e.setDepartment(d); + em.persist(e); + } + d.setEmpMap(empMap); + em.persist(d); + } + + public Employee1 createEmployee1(EntityManager em, int id) { + Employee1 e = new Employee1(); + e.setEmpId(id); + return e; + } + + public void createDepartment2(EntityManager em, int id) { + Department2 d = new Department2(); + d.setDeptId(id); + for (int i = 0; i < numEmployeesPerDept; i++) { + Employee2 e = createEmployee2(em, empId++); + d.addEmployee(e); + e.setDepartment(d); + em.persist(e); + } + em.persist(d); + } + + public Employee2 createEmployee2(EntityManager em, int id) { + Employee2 e = new Employee2("e" + id, new Date()); + return e; + } + + public void createDepartment3(EntityManager em, int id) { + Department3 d = new Department3(); + d.setDeptId(id); + for (int i = 0; i < numEmployeesPerDept; i++) { + Employee3 e = createEmployee3(em, empId++); + d.addEmployee(e); + e.setDepartment(d); + em.persist(e); + } + em.persist(d); + } + + public Employee3 createEmployee3(EntityManager em, int id) { + Employee3 e = new Employee3(); + EmployeeName3 name = new EmployeeName3("f" + id, "l" + id); + e.setEmpId(id); + e.setName(name); + return e; + } + + public void findObj() { + EntityManager em = emf.createEntityManager(); + Department1 d1 = em.find(Department1.class, 1); + assertDepartment1(d1); + + Employee1 e1 = em.find(Employee1.class, 1); + assertEmployee1(e1); + + Department2 d2 = em.find(Department2.class, 3); + assertDepartment2(d2); + + Map empMap = d2.getEmpMap(); + Set<EmployeePK2> keys = empMap.keySet(); + for (EmployeePK2 key : keys) { + Employee2 e2 = em.find(Employee2.class, key); + assertEmployee2(e2); + } + + Department3 d3 = em.find(Department3.class, 5); + assertDepartment3(d3); + + Employee3 e3 = em.find(Employee3.class, 9); + assertEmployee3(e3); + + em.close(); + } + + public void assertDepartment1(Department1 d) { + int id = d.getDeptId(); + Map<Integer, Employee1> es = d.getEmpMap(); + Assert.assertEquals(2,es.size()); + Set keys = es.keySet(); + for (Object obj : keys) { + Integer empId = (Integer) obj; + Employee1 e = es.get(empId); + Assert.assertEquals(empId.intValue(), e.getEmpId()); + } + } + + public void assertDepartment2(Department2 d) { + int id = d.getDeptId(); + Map<EmployeePK2, Employee2> es = d.getEmpMap(); + Assert.assertEquals(2,es.size()); + Set<EmployeePK2> keys = es.keySet(); + for (EmployeePK2 pk : keys) { + Employee2 e = es.get(pk); + Assert.assertEquals(pk, e.getEmpPK()); + } + } + + public void assertDepartment3(Department3 d) { + int id = d.getDeptId(); + Map<EmployeeName3, Employee3> es = d.getEmployees(); + Assert.assertEquals(2,es.size()); + Set<EmployeeName3> keys = es.keySet(); + for (EmployeeName3 key : keys) { + Employee3 e = es.get(key); + Assert.assertEquals(key, e.getName()); + } + } + + public void assertEmployee1(Employee1 e) { + int id = e.getEmpId(); + Department1 d = e.getDepartment(); + assertDepartment1(d); + } + + public void assertEmployee2(Employee2 e) { + EmployeePK2 pk = e.getEmpPK(); + Department2 d = e.getDepartment(); + assertDepartment2(d); + } + + public void assertEmployee3(Employee3 e) { + int id = e.getEmpId(); + Department3 d = e.getDepartment(); + assertDepartment3(d); + } + + public void queryObj() { + queryDepartment1(emf); + queryEmployee1(emf); + queryDepartment2(emf); + queryEmployee2(emf); + queryDepartment3(emf); + queryEmployee3(emf); + } + + public void queryDepartment1(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select d from Department1 d"); + List<Department1> ds = q.getResultList(); + for (Department1 d : ds) + assertDepartment1(d); + + tran.commit(); + em.close(); + } + + public void queryEmployee1(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select e from Employee1 e"); + List<Employee1> es = q.getResultList(); + for (Employee1 e : es) + assertEmployee1(e); + + tran.commit(); + em.close(); + } + + public void queryDepartment2(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select d from Department2 d"); + List<Department2> ds = q.getResultList(); + for (Department2 d : ds) + assertDepartment2(d); + + tran.commit(); + em.close(); + } + + public void queryEmployee2(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select e from Employee2 e"); + List<Employee2> es = q.getResultList(); + for (Employee2 e : es) + assertEmployee2(e); + + tran.commit(); + em.close(); + } + + public void queryDepartment3(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select d from Department3 d"); + List<Department3> ds = q.getResultList(); + for (Department3 d : ds) + assertDepartment3(d); + + tran.commit(); + em.close(); + } + + public void queryEmployee3(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select e from Employee3 e"); + List<Employee3> es = q.getResultList(); + for (Employee3 e : es) + assertEmployee3(e); + + tran.commit(); + em.close(); + } + + public class Listener extends AbstractJDBCListener { + @Override + public void beforeExecuteStatement(JDBCEvent event) { + if (event.getSQL() != null && sql != null) { + sql.add(event.getSQL()); + sqlCount++; + } + } + } +} \ No newline at end of file
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex1/Department.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex1/Department.java?rev=750517&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex1/Department.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex1/Department.java Thu Mar 5 17:30:20 2009 @@ -0,0 +1,60 @@ +/* + * 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.jdbc.maps.spec_10_1_26_ex1; + +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.*; + +...@entity +...@table(name="S26Ex1Dept") +public class Department { + + @Id + int deptId; + + @OneToMany(cascade=CascadeType.ALL, mappedBy="dept", fetch=FetchType.EAGER) + @MapKey(name="empId") + Map<Integer, Employee> empMap = new HashMap<Integer, Employee>(); + + public int getDeptId() { + return deptId; + } + + public void setDeptId(int deptId) { + this.deptId = deptId; + } + + public Map<Integer, Employee> getEmpMap() { + return empMap; + } + + public void setEmpMap(Map<Integer, Employee> empMap) { + this.empMap = empMap; + } + + public void addEmployee(Employee e) { + empMap.put(e.getEmpId(), e); + } + + public void removeEmployee(Integer empId) { + empMap.remove(empId); + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex1/Employee.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex1/Employee.java?rev=750517&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex1/Employee.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex1/Employee.java Thu Mar 5 17:30:20 2009 @@ -0,0 +1,48 @@ +/* + * 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.jdbc.maps.spec_10_1_26_ex1; + +import javax.persistence.*; + +...@entity +...@table(name="S26Ex1Emp") +public class Employee { + @Id + int empId; + + @ManyToOne + @JoinColumn(name="dept_id") + Department dept; + + public int getEmpId() { + return empId; + } + + public void setEmpId(int empId) { + this.empId = empId; + } + + public Department getDepartment() { + return dept; + } + + public void setDepartment(Department department) { + this.dept = department; + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex1/TestSpec10_1_26_Ex1.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex1/TestSpec10_1_26_Ex1.java?rev=750517&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex1/TestSpec10_1_26_Ex1.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex1/TestSpec10_1_26_Ex1.java Thu Mar 5 17:30:20 2009 @@ -0,0 +1,229 @@ +/* + * 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.jdbc.maps.spec_10_1_26_ex1; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.persistence.EntityManager; +import javax.persistence.EntityTransaction; +import javax.persistence.Query; + +import junit.framework.Assert; + +import org.apache.openjpa.lib.jdbc.AbstractJDBCListener; +import org.apache.openjpa.lib.jdbc.JDBCEvent; +import org.apache.openjpa.lib.jdbc.JDBCListener; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +public class TestSpec10_1_26_Ex1 extends SingleEMFTestCase { + + public int numDepartments = 2; + public int numEmployeesPerDept = 2; + public List<String> namedQueries = new ArrayList<String>(); + + public int deptId = 1; + public int empId = 1; + + protected List<String> sql = new ArrayList<String>(); + protected int sqlCount; + + public void setUp() { + super.setUp(CLEAR_TABLES, + Department.class, + Employee.class, + "openjpa.jdbc.JDBCListeners", + new JDBCListener[] { + this.new Listener() + }); + createObj(); + } + + public void testQualifiedId() throws Exception { + EntityManager em = emf.createEntityManager(); + String query = "select KEY(e) from Department d, " + + " in (d.empMap) e order by d.deptId, e.empId"; + System.err.println(query); + List rs = em.createQuery(query).getResultList(); + Integer d = (Integer) rs.get(0); + + em.clear(); + String query4 = "select ENTRY(e) from Department d, " + + " in (d.empMap) e order by d.deptId, e.empId"; + List rs4 = em.createQuery(query4).getResultList(); + Map.Entry me = (Map.Entry) rs4.get(0); + + assertTrue(d.equals(me.getKey())); + + em.close(); + } + + public void testQueryObject() throws Exception { + queryObj(); + findObj(); + } + + public List<String> getSql() { + return sql; + } + + public int getSqlCount() { + return sqlCount; + } + + public void createObj() { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + for (int i = 0; i < numDepartments; i++) + createDepartment(em, deptId++); + tran.begin(); + em.flush(); + tran.commit(); + em.close(); + } + + public void createDepartment(EntityManager em, int id) { + Department d = new Department(); + d.setDeptId(id); + Map emps = new HashMap(); + for (int i = 0; i < numEmployeesPerDept; i++) { + Employee e = createEmployee(em, empId++); + d.addEmployee(e); + emps.put(e.getEmpId(), e); + e.setDepartment(d); + em.persist(e); + } + em.persist(d); + } + + public Employee createEmployee(EntityManager em, int id) { + Employee e = new Employee(); + e.setEmpId(id); + return e; + } + + public void findObj() { + EntityManager em = emf.createEntityManager(); + Department d = em.find(Department.class, 1); + assertDepartment(d); + + Employee e = em.find(Employee.class, 1); + assertEmployee(e); + + // updateObj by adding a new Employee + updateObj(em, d); + + deleteObj(em, d); + em.close(); + } + + public void updateObj(EntityManager em, Department d) { + EntityTransaction tran = em.getTransaction(); + + // add an element + tran.begin(); + Employee e = createEmployee(em, + numDepartments * numEmployeesPerDept + 1); + d.addEmployee(e); + e.setDepartment(d); + em.persist(d); + em.persist(e); + em.flush(); + tran.commit(); + + // remove an element + tran.begin(); + d.removeEmployee(e.getEmpId()); + e.setDepartment(null); + em.persist(d); + em.persist(e); + em.flush(); + tran.commit(); + + } + + public void deleteObj(EntityManager em, Department d) { + EntityTransaction tran = em.getTransaction(); + tran.begin(); + em.remove(d); + tran.commit(); + } + + public void assertDepartment(Department d) { + int id = d.getDeptId(); + Map<Integer, Employee> es = d.getEmpMap(); + Assert.assertEquals(2, es.size()); + Set keys = es.keySet(); + for (Object obj : keys) { + Integer empId = (Integer) obj; + Employee e = es.get(empId); + Assert.assertEquals(empId.intValue(), e.getEmpId()); + } + } + + public void queryObj() { + queryDepartment(); + queryEmployee(); + } + + public void queryDepartment() { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select d from Department d"); + List<Department> ds = q.getResultList(); + for (Department d : ds) + assertDepartment(d); + + tran.commit(); + em.close(); + } + + public void queryEmployee() { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select e from Employee e"); + List<Employee> es = q.getResultList(); + for (Employee e : es) + assertEmployee(e); + + tran.commit(); + em.close(); + } + + public void assertEmployee(Employee e) { + int id = e.getEmpId(); + Department d = e.getDepartment(); + assertDepartment(d); + } + + public class Listener extends AbstractJDBCListener { + @Override + public void beforeExecuteStatement(JDBCEvent event) { + if (event.getSQL() != null && sql != null) { + sql.add(event.getSQL()); + sqlCount++; + } + } + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex2/Department.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex2/Department.java?rev=750517&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex2/Department.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex2/Department.java Thu Mar 5 17:30:20 2009 @@ -0,0 +1,59 @@ +/* + * 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.jdbc.maps.spec_10_1_26_ex2; + +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.*; + +...@entity +...@table(name="S26Ex2Dept") +public class Department { + + int deptId; + Map<EmployeePK, Employee> empMap = new HashMap<EmployeePK, Employee>(); + + @Id + public int getDeptId() { + return deptId; + } + + public void setDeptId(int deptId) { + this.deptId = deptId; + } + + @OneToMany(cascade=CascadeType.ALL,mappedBy="department") + @MapKey(name="empPK") + public Map<EmployeePK, Employee> getEmpMap() { + return empMap; + } + + public void setEmpMap(Map<EmployeePK, Employee> empMap) { + this.empMap = empMap; + } + + public void addEmployee(Employee e) { + empMap.put(e.getEmpPK(), e); + } + + public void removeEmployee(EmployeePK pk) { + empMap.remove(pk); + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex2/Employee.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex2/Employee.java?rev=750517&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex2/Employee.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex2/Employee.java Thu Mar 5 17:30:20 2009 @@ -0,0 +1,56 @@ +/* + * 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.jdbc.maps.spec_10_1_26_ex2; + +import java.util.Date; + +import javax.persistence.*; + +...@entity +...@table(name="S26Ex2Emp") +public class Employee { + EmployeePK empPK; + + Department department; + + public Employee() {} + + public Employee(String name, Date bDate) { + this.empPK = new EmployeePK(name, bDate); + } + + @EmbeddedId + public EmployeePK getEmpPK() { + return empPK; + } + + public void setEmpPK(EmployeePK empPK) { + this.empPK = empPK; + } + + @ManyToOne + @JoinColumn(name="dept_id") + public Department getDepartment() { + return department; + } + + public void setDepartment(Department department) { + this.department = department; + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex2/EmployeePK.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex2/EmployeePK.java?rev=750517&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex2/EmployeePK.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex2/EmployeePK.java Thu Mar 5 17:30:20 2009 @@ -0,0 +1,55 @@ +/* + * 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.jdbc.maps.spec_10_1_26_ex2; + +import java.io.Serializable; +import java.util.Date; + +import javax.persistence.*; + +...@embeddable +public class EmployeePK implements Serializable { + String name; + Date bDay; + + public EmployeePK() {} + public EmployeePK(String name, Date bDay) { + this.name = name; + this.bDay = bDay; + } + + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof EmployeePK)) + return false; + EmployeePK pk = (EmployeePK) o; + if (pk.name.equals(name) && + pk.bDay.equals(bDay)) + return true; + return false; + } + + public int hashCode() { + int code = 0; + code += name.hashCode(); + code += bDay.hashCode(); + return code; + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex2/TestSpec10_1_26_Ex2.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex2/TestSpec10_1_26_Ex2.java?rev=750517&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex2/TestSpec10_1_26_Ex2.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex2/TestSpec10_1_26_Ex2.java Thu Mar 5 17:30:20 2009 @@ -0,0 +1,226 @@ +/* + * 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.jdbc.maps.spec_10_1_26_ex2; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Query; + +import junit.framework.Assert; + +import org.apache.openjpa.lib.jdbc.AbstractJDBCListener; +import org.apache.openjpa.lib.jdbc.JDBCEvent; +import org.apache.openjpa.lib.jdbc.JDBCListener; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +public class TestSpec10_1_26_Ex2 extends SingleEMFTestCase { + public int numDepartments = 2; + public int numEmployeesPerDept = 2; + public List<String> namedQueries = new ArrayList<String>(); + + public int deptId = 1; + public int empId = 1; + + protected List<String> sql = new ArrayList<String>(); + protected int sqlCount; + + public void setUp() { + super.setUp(CLEAR_TABLES, + Department.class, + Employee.class, + EmployeePK.class, + "openjpa.jdbc.JDBCListeners", + new JDBCListener[] { + this.new Listener() + }); + createObj(emf); + } + + public void testQualifiedId() throws Exception { + EntityManager em = emf.createEntityManager(); + String query = "select KEY(e) from Department d, " + + " in (d.empMap) e where d.deptId = 1"; + List rs = em.createQuery(query).getResultList(); + EmployeePK d = (EmployeePK) rs.get(0); + + em.clear(); + String query4 = "select ENTRY(e) from Department d, " + + " in (d.empMap) e where d.deptId = 1"; + List rs4 = em.createQuery(query4).getResultList(); + Map.Entry me = (Map.Entry) rs4.get(0); + + assertTrue(d.equals(me.getKey())); + + em.close(); + } + + public void testQueryObject() { + queryObj(emf); + } + + public List<String> getSql() { + return sql; + } + + public int getSqlCount() { + return sqlCount; + } + + public void createObj(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + for (int i = 0; i < numDepartments; i++) + createDepartment(em, deptId++); + tran.begin(); + em.flush(); + tran.commit(); + em.close(); + } + + public void createDepartment(EntityManager em, int id) { + Department d = new Department(); + d.setDeptId(id); + for (int i = 0; i < numEmployeesPerDept; i++) { + Employee e = createEmployee(em, empId++); + d.addEmployee(e); + e.setDepartment(d); + em.persist(e); + } + em.persist(d); + } + + public Employee createEmployee(EntityManager em, int id) { + Employee e = new Employee("e" + id, new Date()); + return e; + } + + public void findObj(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + Department d = em.find(Department.class, 1); + assertDepartment(d); + + Map emps = d.getEmpMap(); + Set<EmployeePK> keys = emps.keySet(); + for (EmployeePK key : keys) { + Employee e = em.find(Employee.class, key); + assertEmployee(e); + } + + // updateObj by adding a new Employee + updateObj(em, d); + deleteObj(em, d); + em.close(); + } + + public void updateObj(EntityManager em, Department d) { + EntityTransaction tran = em.getTransaction(); + + // add an element + tran.begin(); + Employee e = createEmployee(em, numDepartments * numEmployeesPerDept + + 1); + d.addEmployee(e); + e.setDepartment(d); + em.persist(d); + em.persist(e); + em.flush(); + tran.commit(); + + // remove an element + tran.begin(); + d.removeEmployee(e.getEmpPK()); + e.setDepartment(null); + em.persist(d); + em.persist(e); + em.flush(); + tran.commit(); + } + + public void deleteObj(EntityManager em, Department d) { + EntityTransaction tran = em.getTransaction(); + tran.begin(); + em.remove(d); + tran.commit(); + } + + public void assertDepartment(Department d) { + int id = d.getDeptId(); + Map es = d.getEmpMap(); + Assert.assertEquals(2, es.size()); + Set keys = es.keySet(); + for (Object obj : keys) { + EmployeePK empPK = (EmployeePK) obj; + Employee e = (Employee) es.get(empPK); + Assert.assertEquals(empPK, e.getEmpPK()); + } + } + + public void queryObj(EntityManagerFactory emf) { + queryDepartment(emf); + queryEmployee(emf); + } + + public void queryDepartment(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select d from Department d"); + List<Department> ds = q.getResultList(); + for (Department d : ds) { + assertDepartment(d); + } + tran.commit(); + em.close(); + } + + public void queryEmployee(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select e from Employee e"); + List<Employee> es = q.getResultList(); + for (Employee e : es) { + assertEmployee(e); + } + tran.commit(); + em.close(); + } + + public void assertEmployee(Employee e) { + EmployeePK pk = e.getEmpPK(); + Department d = e.getDepartment(); + } + + public class Listener extends AbstractJDBCListener { + @Override + public void beforeExecuteStatement(JDBCEvent event) { + if (event.getSQL() != null && sql != null) { + sql.add(event.getSQL()); + sqlCount++; + } + } + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex3/Department.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex3/Department.java?rev=750517&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex3/Department.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex3/Department.java Thu Mar 5 17:30:20 2009 @@ -0,0 +1,61 @@ +/* + * 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.jdbc.maps.spec_10_1_26_ex3; + +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.MapKey; +import javax.persistence.OneToMany; +import javax.persistence.Table; + +...@entity +...@table(name="S26Ex3Dept") +public class Department { + + @Id + int deptId; + + @OneToMany(cascade=CascadeType.ALL) + @MapKey(name="name") + Map<EmployeeName, Employee> emps = new HashMap<EmployeeName, Employee>(); + + public int getDeptId() { + return deptId; + } + + public void setDeptId(int deptId) { + this.deptId = deptId; + } + + public Map<EmployeeName, Employee> getEmployees() { + return emps; + } + + public void addEmployee(Employee emp) { + emps.put(emp.getName(), emp); + } + + public void removeEmployee(EmployeeName name) { + emps.remove(name); + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex3/Employee.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex3/Employee.java?rev=750517&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex3/Employee.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex3/Employee.java Thu Mar 5 17:30:20 2009 @@ -0,0 +1,59 @@ +/* + * 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.jdbc.maps.spec_10_1_26_ex3; + +import javax.persistence.*; + +...@entity +...@table(name="S26Ex3Emp") +public class Employee { + @Id + int empId; + + @ManyToOne + @JoinColumn(name="dept_id") + Department department; + + @Embedded + EmployeeName name; + + public int getEmpId() { + return empId; + } + + public void setEmpId(int empId) { + this.empId = empId; + } + + public Department getDepartment() { + return department; + } + + public void setDepartment(Department department) { + this.department = department; + } + + public EmployeeName getName() { + return name; + } + + public void setName(EmployeeName name) { + this.name = name; + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex3/EmployeeName.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex3/EmployeeName.java?rev=750517&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex3/EmployeeName.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex3/EmployeeName.java Thu Mar 5 17:30:20 2009 @@ -0,0 +1,68 @@ +/* + * 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.jdbc.maps.spec_10_1_26_ex3; + +import javax.persistence.Embeddable; + +...@embeddable +public class EmployeeName { + + String fName; + String lName; + + public EmployeeName() {} + + public EmployeeName(String fName, String lName) { + this.fName = fName; + this.lName = lName; + } + + public String getFName() { + return fName; + } + + public void setFName(String fName) { + this.fName = fName; + } + + public String getLName() { + return lName; + } + + public void setLName(String lName) { + this.lName = lName; + } + + public boolean equals(Object o) { + if (!(o instanceof EmployeeName)) + return false; + EmployeeName other = (EmployeeName) o; + if (fName.equals(other.fName) && + lName.equals(other.lName)) + return true; + return false; + } + + public int hashCode() { + int ret = 0; + ret += lName.hashCode(); + ret = 31 * ret + fName.hashCode(); + return ret; + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex3/TestSpec10_1_26_Ex3.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex3/TestSpec10_1_26_Ex3.java?rev=750517&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex3/TestSpec10_1_26_Ex3.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex3/TestSpec10_1_26_Ex3.java Thu Mar 5 17:30:20 2009 @@ -0,0 +1,223 @@ +/* + * 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.jdbc.maps.spec_10_1_26_ex3; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Query; + +import junit.framework.Assert; + +import org.apache.openjpa.lib.jdbc.AbstractJDBCListener; +import org.apache.openjpa.lib.jdbc.JDBCEvent; +import org.apache.openjpa.lib.jdbc.JDBCListener; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +public class TestSpec10_1_26_Ex3 extends SingleEMFTestCase { + + public int numDepartments = 2; + public int numEmployeesPerDept = 2; + public List<String> namedQueries = new ArrayList<String>(); + + public int deptId = 1; + public int empId = 1; + + protected List<String> sql = new ArrayList<String>(); + protected int sqlCount; + + public void setUp() { + super.setUp(DROP_TABLES, + Department.class, + Employee.class, + EmployeeName.class, + "openjpa.jdbc.JDBCListeners", + new JDBCListener[] { + this.new Listener() + }); + createObj(emf); + } + + public void testQualifiedId() throws Exception { + EntityManager em = emf.createEntityManager(); + String query = "select KEY(e) from Department d, " + + " in (d.emps) e order by d.deptId, e.empId"; + List rs = em.createQuery(query).getResultList(); + EmployeeName d = (EmployeeName) rs.get(0); + + em.clear(); + String query4 = "select ENTRY(e) from Department d, " + + " in (d.emps) e order by d.deptId, e.empId"; + List rs4 = em.createQuery(query4).getResultList(); + Map.Entry me = (Map.Entry) rs4.get(0); + + assertTrue(d.equals(me.getKey())); + + em.close(); + } + + public void testQueryObject() { + queryObj(emf); + } + + public List<String> getSql() { + return sql; + } + + public int getSqlCount() { + return sqlCount; + } + + public void createObj(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + for (int i = 0; i < numDepartments; i++) + createDepartment(em, deptId++); + tran.begin(); + em.flush(); + tran.commit(); + em.close(); + } + + public void createDepartment(EntityManager em, int id) { + Department d = new Department(); + d.setDeptId(id); + for (int i = 0; i < numEmployeesPerDept; i++) { + Employee e = createEmployee(em, empId++); + d.addEmployee(e); + e.setDepartment(d); + em.persist(e); + } + em.persist(d); + } + + public Employee createEmployee(EntityManager em, int id) { + Employee e = new Employee(); + EmployeeName name = new EmployeeName("f" + id, "l" + id); + e.setEmpId(id); + e.setName(name); + return e; + } + + public void findObj(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + Department d = em.find(Department.class, 1); + assertDepartment(d); + + //Employee e = em.find(Employee.class, 1); + //assertEmployee(e); + + updateObj(em, d); + deleteObj(em, d); + em.close(); + } + + public void updateObj(EntityManager em, Department d) { + EntityTransaction tran = em.getTransaction(); + // add an element + tran.begin(); + Employee e = createEmployee(em, numDepartments * numEmployeesPerDept + 1); + d.addEmployee(e); + e.setDepartment(d); + em.persist(d); + em.persist(e); + em.flush(); + tran.commit(); + + // remove an element + tran.begin(); + d.removeEmployee(e.getName()); + e.setDepartment(null); + em.persist(d); + em.flush(); + tran.commit(); + } + + public void deleteObj(EntityManager em, Department d) { + EntityTransaction tran = em.getTransaction(); + tran.begin(); + em.remove(d); + tran.commit(); + } + + public void assertDepartment(Department d) { + int id = d.getDeptId(); + Map<EmployeeName, Employee> es = d.getEmployees(); + Assert.assertEquals(2,es.size()); + Set keys = es.keySet(); + for (Object obj : keys) { + EmployeeName empName = (EmployeeName) obj; + Employee e = (Employee)es.get(empName); + Assert.assertEquals(empName, e.getName()); + } + } + + public void queryObj(EntityManagerFactory emf) { + queryDepartment(emf); + queryEmployee(emf); + } + + public void queryDepartment(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select d from Department d"); + List<Department> ds = q.getResultList(); + for (Department d : ds){ + assertDepartment(d); + } + tran.commit(); + em.close(); + } + + public void queryEmployee(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select e from Employee e"); + List<Employee> es = q.getResultList(); + for (Employee e : es){ + assertEmployee(e); + } + tran.commit(); + em.close(); + } + + public void assertEmployee(Employee e) { + int id = e.getEmpId(); + Department d = e.getDepartment(); + assertDepartment(d); + } + + public class Listener extends AbstractJDBCListener { + @Override + public void beforeExecuteStatement(JDBCEvent event) { + if (event.getSQL() != null && sql != null) { + sql.add(event.getSQL()); + sqlCount++; + } + } + } +} +
