You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by fa...@apache.org on 2009/03/05 18:30:25 UTC
svn commit: r750517 [3/4] - in /openjpa/trunk:
openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/
openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/
openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/
openjpa-kernel/src/...
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex6/TestMany2ManyMapEx6.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex6/TestMany2ManyMapEx6.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex6/TestMany2ManyMapEx6.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex6/TestMany2ManyMapEx6.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.m2mmapex6;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+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 TestMany2ManyMapEx6 extends SingleEMFTestCase {
+
+ public int numEmployees = 2;
+ public int numPhoneNumbers = numEmployees + 1;
+ public int numEmployeesPerPhoneNumber = 2;
+ public int numPhoneNumbersPerEmployee = 2;
+ public Map<Integer, PhoneNumber> phones =
+ new HashMap<Integer, PhoneNumber>();
+ public List<String> namedQueries = new ArrayList<String>();
+
+ public Map<Integer, Employee> empMap = new HashMap<Integer, Employee>();
+ public Map<Integer, PhoneNumber> phoneMap =
+ new HashMap<Integer, PhoneNumber>();
+
+ public int empId = 1;
+ public int phoneId = 1;
+ public int divId = 1;
+ public int deptId = 1;
+
+ protected List<String> sql = new ArrayList<String>();
+ protected int sqlCount;
+
+ public void setUp() {
+ super.setUp(CLEAR_TABLES,
+ Employee.class,
+ PhoneNumber.class,
+ FullName.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 PhoneNumber p, " +
+ " in (p.emps) e order by e.empId";
+ List rs = em.createQuery(query).getResultList();
+ FullName d = (FullName) rs.get(0);
+
+ String query2 = "select KEY(p) from Employee e, " +
+ " in (e.phones) p";
+ List rs2 = em.createQuery(query2).getResultList();
+ String k = (String) rs2.get(0);
+
+ em.clear();
+ String query4 = "select ENTRY(e) from PhoneNumber p, " +
+ " in (p.emps) e order by 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 < numEmployees; i++) {
+ Employee e = createEmployee(em, empId++);
+ empMap.put(e.getEmpId(), e);
+ }
+ tran.begin();
+ em.flush();
+ tran.commit();
+ em.close();
+ }
+
+ public Employee createEmployee(EntityManager em, int id) {
+ Employee e = new Employee();
+ e.setEmpId(id);
+ for (int i = 0; i < numPhoneNumbersPerEmployee; i++) {
+ PhoneNumber phoneNumber = new PhoneNumber();
+ phoneNumber.setNumber(phoneId++);
+ FullName name = new FullName("f" + id, "l" + id);
+ phoneNumber.addEmployees(name, e);
+ e.addPhoneNumber("String" + phoneNumber.getNumber(), phoneNumber);
+ em.persist(phoneNumber);
+ phoneMap.put(phoneNumber.getNumber(), phoneNumber);
+ }
+ em.persist(e);
+ return e;
+ }
+
+ public void findObj() throws Exception {
+ EntityManager em = emf.createEntityManager();
+ Employee e = em.find(Employee.class, 1);
+ assertEmployee(e);
+
+ PhoneNumber p = em.find(PhoneNumber.class, 1);
+ assertPhoneNumber(p);
+ em.close();
+ }
+
+ public void queryObj() throws Exception {
+ queryEmployee();
+ queryPhoneNumber();
+ }
+
+ public void queryPhoneNumber() throws Exception {
+ EntityManager em = emf.createEntityManager();
+ EntityTransaction tran = em.getTransaction();
+ tran.begin();
+ Query q = em.createQuery("select p from PhoneNumber p");
+ List<PhoneNumber> ps = q.getResultList();
+ for (PhoneNumber p : ps){
+ assertPhoneNumber(p);
+ }
+ tran.commit();
+ em.close();
+ }
+
+ public void queryEmployee() throws Exception {
+ 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) throws Exception {
+ int id = e.getEmpId();
+ Employee e0 = empMap.get(id);
+ Map<String, PhoneNumber> phones = e.getPhoneNumbers();
+ Map<String, PhoneNumber> phones0 = e0.getPhoneNumbers();
+ Assert.assertEquals(phones0.size(), phones.size());
+ checkPhoneMap(phones0, phones);
+ }
+
+ public void assertPhoneNumber(PhoneNumber p) throws Exception {
+ int number = p.getNumber();
+ PhoneNumber p0 = phoneMap.get(number);
+ Map<FullName, Employee> es = p.getEmployees();
+ Map<FullName, Employee> es0 = p0.getEmployees();
+ Assert.assertEquals(es0.size(), es.size());
+ checkEmpMap(es0, es);
+ }
+
+ public void checkPhoneMap(Map<String, PhoneNumber> es0,
+ Map<String, PhoneNumber> es) throws Exception {
+ Collection<Map.Entry<String, PhoneNumber>> entrySets0 = es0.entrySet();
+ for (Map.Entry<String, PhoneNumber> entry0 : entrySets0) {
+ String key0 = entry0.getKey();
+ PhoneNumber p0 = entry0.getValue();
+ PhoneNumber p = es.get(key0);
+ if (!p0.equals(p))
+ throw new Exception("Assertion Failure");
+
+ }
+ }
+
+ public void checkEmpMap(Map<FullName, Employee> es0,
+ Map<FullName, Employee> es) throws Exception {
+ Collection<Map.Entry<FullName, Employee>> entrySets0 = es0.entrySet();
+ for (Map.Entry<FullName, Employee> entry0 : entrySets0) {
+ FullName key0 = entry0.getKey();
+ Employee e0 = entry0.getValue();
+ Employee e = es.get(key0);
+ if (!e0.equals(e))
+ throw new Exception("Assertion failure");
+ }
+ }
+
+ 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/m2mmapex7/Division.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex7/Division.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex7/Division.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex7/Division.java Thu Mar 5 17:30:20 2009
@@ -0,0 +1,65 @@
+/*
+ * 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.m2mmapex7;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+@Table(name="MEx7Division")
+public class Division {
+ @Id
+ int id;
+
+ String name;
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public boolean equals(Object o) {
+ if (!(o instanceof Division))
+ return false;
+ Division other = (Division) o;
+ if (name.equals(other.name) &&
+ id == other.id)
+ return true;
+ return false;
+ }
+
+ public int hashCode() {
+ int ret = 0;
+ ret = ret * 31 + name.hashCode();
+ ret = ret *31 + id;
+ return ret;
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex7/Employee.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex7/Employee.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex7/Employee.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex7/Employee.java Thu Mar 5 17:30:20 2009
@@ -0,0 +1,70 @@
+/*
+ * 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.m2mmapex7;
+
+import javax.persistence.*;
+
+import java.util.*;
+
+@Entity
+@Table(name="MEx7Emp")
+public class Employee {
+ @Id
+ int empId;
+
+ @ManyToMany // Bidirectional
+ Map<FullName, PhoneNumber> phones = new HashMap<FullName, PhoneNumber>();
+
+ public Map<FullName, PhoneNumber> getPhoneNumbers() {
+ return phones;
+ }
+
+ public void addPhoneNumber(FullName name, PhoneNumber phoneNumber) {
+ phones.put(name, phoneNumber);
+ }
+
+ public void removePhoneNumber(FullName name) {
+ phones.remove(name);
+ }
+
+ public int getEmpId() {
+ return empId;
+ }
+
+ public void setEmpId(int empId) {
+ this.empId = empId;
+ }
+
+ public boolean equals(Object o) {
+ Employee e = (Employee) o;
+ Map<FullName, PhoneNumber> map = e.getPhoneNumbers();
+ if (map.size() != phones.size())
+ return false;
+ Collection<Map.Entry<FullName, PhoneNumber>> entries =
+ (Collection<Map.Entry<FullName, PhoneNumber>>) phones.entrySet();
+ for (Map.Entry<FullName, PhoneNumber> entry : entries) {
+ FullName key = entry.getKey();
+ PhoneNumber p = entry.getValue();
+ PhoneNumber p0 = map.get(key);
+ if (p.getNumber() != p0.getNumber())
+ return false;
+ }
+ return true;
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex7/FullName.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex7/FullName.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex7/FullName.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex7/FullName.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.m2mmapex7;
+
+import javax.persistence.Embeddable;
+
+@Embeddable
+public class FullName {
+
+ String fName;
+ String lName;
+
+ public FullName() {}
+
+ public FullName(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 FullName))
+ return false;
+ FullName other = (FullName) 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/m2mmapex7/PhoneNumber.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex7/PhoneNumber.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex7/PhoneNumber.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex7/PhoneNumber.java Thu Mar 5 17:30:20 2009
@@ -0,0 +1,69 @@
+/*
+ * 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.m2mmapex7;
+
+import javax.persistence.*;
+
+import java.util.*;
+
+@Entity
+@Table(name="MEx7Phone")
+public class PhoneNumber {
+ @Id int number;
+
+ @ManyToMany(mappedBy="phones")
+ Map<Division, Employee> emps = new HashMap<Division, Employee>();
+
+ public int getNumber() {
+ return number;
+ }
+
+ public void setNumber(int number) {
+ this.number = number;
+ }
+
+ public Map<Division, Employee> getEmployees() {
+ return emps;
+ }
+
+ public void addEmployees(Division d, Employee employee) {
+ emps.put(d, employee);
+ }
+
+ public void removeEmployee(Division d) {
+ emps.remove(d);
+ }
+
+ public boolean equals(Object o) {
+ PhoneNumber p = (PhoneNumber) o;
+ Map<Division, Employee> map = p.getEmployees();
+ if (p.getEmployees().size() != emps.size())
+ return false;
+ Collection<Map.Entry<Division, Employee>> entries =
+ (Collection<Map.Entry<Division, Employee>>) emps.entrySet();
+ for (Map.Entry<Division, Employee> entry : entries) {
+ Division key = entry.getKey();
+ Employee e = map.get(key);
+ Employee e0 = entry.getValue();
+ if (e.getEmpId() != e0.getEmpId())
+ return false;
+ }
+ return true;
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex7/TestMany2ManyMapEx7.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex7/TestMany2ManyMapEx7.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex7/TestMany2ManyMapEx7.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex7/TestMany2ManyMapEx7.java Thu Mar 5 17:30:20 2009
@@ -0,0 +1,240 @@
+/*
+ * 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.m2mmapex7;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+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 TestMany2ManyMapEx7 extends SingleEMFTestCase {
+
+ public int numEmployees = 2;
+ public int numPhoneNumbers = numEmployees + 1;
+ public int numEmployeesPerPhoneNumber = 2;
+ public int numPhoneNumbersPerEmployee = 2;
+ public Map<Integer, PhoneNumber> phones =
+ new HashMap<Integer, PhoneNumber>();
+ public List<String> namedQueries = new ArrayList<String>();
+
+ public Map<Integer, Employee> empMap = new HashMap<Integer, Employee>();
+ public Map<Integer, PhoneNumber> phoneMap =
+ new HashMap<Integer, PhoneNumber>();
+
+ public int empId = 1;
+ public int phoneId = 1;
+ public int divId = 1;
+ public int deptId = 1;
+
+ public void setUp() {
+ super.setUp(CLEAR_TABLES,
+ Division.class,
+ Employee.class,
+ FullName.class,
+ PhoneNumber.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 PhoneNumber p, " +
+ " in (p.emps) e order by e.empId";
+ List rs = em.createQuery(query).getResultList();
+ Division d = (Division) rs.get(0);
+
+ String query2 = "select KEY(p) from Employee e, " +
+ " in (e.phones) p";
+ List rs2 = em.createQuery(query2).getResultList();
+ FullName f = (FullName) rs2.get(0);
+
+ em.clear();
+ String query4 = "select ENTRY(e) from PhoneNumber p, " +
+ " in (p.emps) e order by 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();
+ }
+
+ protected List<String> sql = new ArrayList<String>();
+ protected int sqlCount;
+
+ 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 < numEmployees; i++) {
+ Employee e = createEmployee(em, empId++);
+ empMap.put(e.getEmpId(), e);
+ }
+ tran.begin();
+ em.flush();
+ tran.commit();
+ em.close();
+ }
+
+ public Employee createEmployee(EntityManager em, int id) {
+ Employee e = new Employee();
+ e.setEmpId(id);
+ for (int i = 0; i < numPhoneNumbersPerEmployee; i++) {
+ FullName name = new FullName("f" + id + i, "l" + id + i);
+ PhoneNumber phoneNumber = new PhoneNumber();
+ phoneNumber.setNumber(phoneId++);
+ Division div = createDivision(em, divId++);
+ phoneNumber.addEmployees(div, e);
+ e.addPhoneNumber(name, phoneNumber);
+ em.persist(phoneNumber);
+ em.persist(div);
+ phoneMap.put(phoneNumber.getNumber(), phoneNumber);
+ }
+ em.persist(e);
+ return e;
+ }
+
+ public Division createDivision(EntityManager em, int id) {
+ Division d = new Division();
+ d.setId(id);
+ d.setName("d" + id);
+ return d;
+ }
+
+ public void findObj() throws Exception {
+ EntityManager em = emf.createEntityManager();
+ Employee e = em.find(Employee.class, 1);
+ assertEmployee(e);
+
+ PhoneNumber p = em.find(PhoneNumber.class, 1);
+ assertPhoneNumber(p);
+ em.close();
+ }
+
+ public void queryObj() throws Exception {
+ queryEmployee();
+ queryPhoneNumber();
+ }
+
+ public void queryPhoneNumber() throws Exception {
+ EntityManager em = emf.createEntityManager();
+ EntityTransaction tran = em.getTransaction();
+ tran.begin();
+ Query q = em.createQuery("select p from PhoneNumber p");
+ List<PhoneNumber> ps = q.getResultList();
+ for (PhoneNumber p : ps){
+ assertPhoneNumber(p);
+ }
+ tran.commit();
+ em.close();
+ }
+
+ public void queryEmployee() throws Exception {
+ 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) throws Exception {
+ int id = e.getEmpId();
+ Employee e0 = empMap.get(id);
+ Map<FullName, PhoneNumber> phones = e.getPhoneNumbers();
+ Map<FullName, PhoneNumber> phones0 = e0.getPhoneNumbers();
+ Assert.assertEquals(phones0.size(), phones.size());
+ checkPhoneMap(phones0, phones);
+ }
+
+ public void assertPhoneNumber(PhoneNumber p) throws Exception {
+ int number = p.getNumber();
+ PhoneNumber p0 = phoneMap.get(number);
+ Map<Division, Employee> es = p.getEmployees();
+ Map<Division, Employee> es0 = p0.getEmployees();
+ Assert.assertEquals(es0.size(), es.size());
+ checkEmpMap(es0, es);
+ }
+
+ public void checkPhoneMap(Map<FullName, PhoneNumber> es0,
+ Map<FullName, PhoneNumber> es) throws Exception {
+ Collection<Map.Entry<FullName, PhoneNumber>> entrySets0 =
+ es0.entrySet();
+ for (Map.Entry<FullName, PhoneNumber> entry0 : entrySets0) {
+ FullName key0 = entry0.getKey();
+ PhoneNumber p0 = entry0.getValue();
+ PhoneNumber p = es.get(key0);
+ if (!p0.equals(p))
+ throw new Exception("Assertion Failure");
+
+ }
+ }
+
+ public void checkEmpMap(Map<Division, Employee> es0,
+ Map<Division, Employee> es) throws Exception {
+ Collection<Map.Entry<Division, Employee>> entrySets0 = es0.entrySet();
+ for (Map.Entry<Division, Employee> entry0 : entrySets0) {
+ Division key0 = entry0.getKey();
+ Employee e0 = entry0.getValue();
+ Employee e = es.get(key0);
+ if (!e0.equals(e))
+ throw new Exception("Assertion failure");
+ }
+ }
+
+ 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/m2mmapex8/Employee.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex8/Employee.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex8/Employee.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex8/Employee.java Thu Mar 5 17:30:20 2009
@@ -0,0 +1,70 @@
+/*
+ * 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.m2mmapex8;
+
+import javax.persistence.*;
+
+import java.util.*;
+
+@Entity
+@Table(name="MEx8Emp")
+public class Employee {
+ @Id
+ int empId;
+
+ @ManyToMany // Bidirectional
+ Map<FullName, PhoneNumber> phones = new HashMap<FullName, PhoneNumber>();
+
+ public Map<FullName, PhoneNumber> getPhoneNumbers() {
+ return phones;
+ }
+
+ public void addPhoneNumber(FullName name, PhoneNumber phoneNumber) {
+ phones.put(name, phoneNumber);
+ }
+
+ public void removePhoneNumber(FullName name) {
+ phones.remove(name);
+ }
+
+ public int getEmpId() {
+ return empId;
+ }
+
+ public void setEmpId(int empId) {
+ this.empId = empId;
+ }
+
+ public boolean equals(Object o) {
+ Employee e = (Employee) o;
+ Map<FullName, PhoneNumber> map = e.getPhoneNumbers();
+ if (map.size() != phones.size())
+ return false;
+ Collection<Map.Entry<FullName, PhoneNumber>> entries =
+ (Collection<Map.Entry<FullName, PhoneNumber>>) phones.entrySet();
+ for (Map.Entry<FullName, PhoneNumber> entry : entries) {
+ FullName key = entry.getKey();
+ PhoneNumber p = entry.getValue();
+ PhoneNumber p0 = map.get(key);
+ if (p.getNumber() != p0.getNumber())
+ return false;
+ }
+ return true;
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex8/FullName.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex8/FullName.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex8/FullName.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex8/FullName.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.m2mmapex8;
+
+import javax.persistence.Embeddable;
+
+@Embeddable
+public class FullName {
+
+ String fName;
+ String lName;
+
+ public FullName() {}
+
+ public FullName(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 FullName))
+ return false;
+ FullName other = (FullName) 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/m2mmapex8/PhoneNumber.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex8/PhoneNumber.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex8/PhoneNumber.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex8/PhoneNumber.java Thu Mar 5 17:30:20 2009
@@ -0,0 +1,69 @@
+/*
+ * 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.m2mmapex8;
+
+import javax.persistence.*;
+
+import java.util.*;
+
+@Entity
+@Table(name="MEx8Phone")
+public class PhoneNumber {
+ @Id int number;
+
+ @ManyToMany(mappedBy="phones")
+ Map<String, Employee> emps = new HashMap<String, Employee>();
+
+ public int getNumber() {
+ return number;
+ }
+
+ public void setNumber(int number) {
+ this.number = number;
+ }
+
+ public Map<String, Employee> getEmployees() {
+ return emps;
+ }
+
+ public void addEmployees(String d, Employee employee) {
+ emps.put(d, employee);
+ }
+
+ public void removeEmployee(String d) {
+ emps.remove(d);
+ }
+
+ public boolean equals(Object o) {
+ PhoneNumber p = (PhoneNumber) o;
+ Map<String, Employee> map = p.getEmployees();
+ if (p.getEmployees().size() != emps.size())
+ return false;
+ Collection<Map.Entry<String, Employee>> entries =
+ (Collection<Map.Entry<String, Employee>>) emps.entrySet();
+ for (Map.Entry<String, Employee> entry : entries) {
+ String key = entry.getKey();
+ Employee e = map.get(key);
+ Employee e0 = entry.getValue();
+ if (e.getEmpId() != e0.getEmpId())
+ return false;
+ }
+ return true;
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex8/TestMany2ManyMapEx8.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex8/TestMany2ManyMapEx8.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex8/TestMany2ManyMapEx8.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex8/TestMany2ManyMapEx8.java Thu Mar 5 17:30:20 2009
@@ -0,0 +1,230 @@
+/*
+ * 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.m2mmapex8;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+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 TestMany2ManyMapEx8 extends SingleEMFTestCase {
+
+ public int numEmployees = 2;
+ public int numPhoneNumbers = numEmployees + 1;
+ public int numEmployeesPerPhoneNumber = 2;
+ public int numPhoneNumbersPerEmployee = 2;
+ public Map<Integer, PhoneNumber> phones =
+ new HashMap<Integer, PhoneNumber>();
+ public List<String> namedQueries = new ArrayList<String>();
+
+ public Map<Integer, Employee> empMap = new HashMap<Integer, Employee>();
+ public Map<Integer, PhoneNumber> phoneMap =
+ new HashMap<Integer, PhoneNumber>();
+
+ public int empId = 1;
+ public int phoneId = 1;
+ public int divId = 1;
+ public int deptId = 1;
+
+ protected List<String> sql = new ArrayList<String>();
+ protected int sqlCount;
+
+ public void setUp() {
+ super.setUp(CLEAR_TABLES,
+ Employee.class,
+ FullName.class,
+ PhoneNumber.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 PhoneNumber p, " +
+ " in (p.emps) e order by e.empId";
+ List rs = em.createQuery(query).getResultList();
+ String d = (String) rs.get(0);
+
+ String query2 = "select KEY(p) from Employee e, " +
+ " in (e.phones) p";
+ List rs2 = em.createQuery(query2).getResultList();
+ FullName d2 = (FullName) rs2.get(0);
+
+ em.clear();
+ String query4 = "select ENTRY(e) from PhoneNumber p, " +
+ " in (p.emps) e order by 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 < numEmployees; i++) {
+ Employee e = createEmployee(em, empId++);
+ empMap.put(e.getEmpId(), e);
+ }
+ tran.begin();
+ em.flush();
+ tran.commit();
+ em.close();
+ }
+
+ public Employee createEmployee(EntityManager em, int id) {
+ Employee e = new Employee();
+ e.setEmpId(id);
+ for (int i = 0; i < numPhoneNumbersPerEmployee; i++) {
+ FullName name = new FullName("f" + id + i, "l" + id + i);
+ PhoneNumber phoneNumber = new PhoneNumber();
+ phoneNumber.setNumber(phoneId++);
+ phoneNumber.addEmployees("String" + e.getEmpId() + i, e);
+ e.addPhoneNumber(name, phoneNumber);
+ em.persist(phoneNumber);
+ phoneMap.put(phoneNumber.getNumber(), phoneNumber);
+ }
+ em.persist(e);
+ return e;
+ }
+
+ public void findObj() throws Exception {
+ EntityManager em = emf.createEntityManager();
+ Employee e = em.find(Employee.class, 1);
+ assertEmployee(e);
+
+ PhoneNumber p = em.find(PhoneNumber.class, 1);
+ assertPhoneNumber(p);
+ em.close();
+ }
+
+ public void queryObj() throws Exception {
+ queryEmployee();
+ queryPhoneNumber();
+ }
+
+ public void queryPhoneNumber() throws Exception {
+ EntityManager em = emf.createEntityManager();
+ EntityTransaction tran = em.getTransaction();
+ tran.begin();
+ Query q = em.createQuery("select p from PhoneNumber p");
+ List<PhoneNumber> ps = q.getResultList();
+ for (PhoneNumber p : ps){
+ assertPhoneNumber(p);
+ }
+ tran.commit();
+ em.close();
+ }
+
+ public void queryEmployee() throws Exception {
+ 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) throws Exception {
+ int id = e.getEmpId();
+ Employee e0 = empMap.get(id);
+ Map<FullName, PhoneNumber> phones = e.getPhoneNumbers();
+ Map<FullName, PhoneNumber> phones0 = e0.getPhoneNumbers();
+ Assert.assertEquals(phones0.size(), phones.size());
+ checkPhoneMap(phones0, phones);
+ }
+
+ public void assertPhoneNumber(PhoneNumber p) throws Exception {
+ int number = p.getNumber();
+ PhoneNumber p0 = phoneMap.get(number);
+ Map<String, Employee> es = p.getEmployees();
+ Map<String, Employee> es0 = p0.getEmployees();
+ Assert.assertEquals(es0.size(), es.size());
+ checkEmpMap(es0, es);
+ }
+
+ public void checkPhoneMap(Map<FullName, PhoneNumber> es0,
+ Map<FullName, PhoneNumber> es) throws Exception {
+ Collection<Map.Entry<FullName, PhoneNumber>> entrySets0 =
+ es0.entrySet();
+ for (Map.Entry<FullName, PhoneNumber> entry0 : entrySets0) {
+ FullName key0 = entry0.getKey();
+ PhoneNumber p0 = entry0.getValue();
+ PhoneNumber p = es.get(key0);
+ if (!p0.equals(p))
+ throw new Exception("Assertion Failure");
+
+ }
+ }
+
+ public void checkEmpMap(Map<String, Employee> es0, Map<String, Employee> es)
+ throws Exception {
+ Collection<Map.Entry<String, Employee>> entrySets0 = es0.entrySet();
+ for (Map.Entry<String, Employee> entry0 : entrySets0) {
+ String key0 = entry0.getKey();
+ Employee e0 = entry0.getValue();
+ Employee e = es.get(key0);
+ if (!e0.equals(e))
+ throw new Exception("Assertion failure");
+ }
+ }
+
+ 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/m2mmapex9/Employee.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex9/Employee.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex9/Employee.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex9/Employee.java Thu Mar 5 17:30:20 2009
@@ -0,0 +1,77 @@
+/*
+ * 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.m2mmapex9;
+
+import javax.persistence.*;
+
+
+import java.util.*;
+
+@Entity
+@Table(name="MEx9Emp")
+public class Employee {
+ @Id
+ int empId;
+
+ @ManyToMany
+ //@AttributeOverrides({
+ // @AttributeOverride(name="fName", column=@Column(name="fName_Emp")),
+ // @AttributeOverride(name="lName", column=@Column(name="lName_Emp"))
+ //})
+ Map<FullPhoneName, PhoneNumber> phones =
+ new HashMap<FullPhoneName, PhoneNumber>(); // Bidirectional
+
+ public Map<FullPhoneName, PhoneNumber> getPhoneNumbers() {
+ return phones;
+ }
+
+ public void addPhoneNumber(FullPhoneName name, PhoneNumber phoneNumber) {
+ phones.put(name, phoneNumber);
+ }
+
+ public void removePhoneNumber(FullPhoneName name) {
+ phones.remove(name);
+ }
+
+ public int getEmpId() {
+ return empId;
+ }
+
+ public void setEmpId(int empId) {
+ this.empId = empId;
+ }
+
+ public boolean equals(Object o) {
+ Employee e = (Employee) o;
+ Map<FullPhoneName, PhoneNumber> map = e.getPhoneNumbers();
+ if (map.size() != phones.size())
+ return false;
+ Collection<Map.Entry<FullPhoneName, PhoneNumber>> entries =
+ (Collection<Map.Entry<FullPhoneName, PhoneNumber>>)
+ phones.entrySet();
+ for (Map.Entry<FullPhoneName, PhoneNumber> entry : entries) {
+ FullPhoneName key = entry.getKey();
+ PhoneNumber p = entry.getValue();
+ PhoneNumber p0 = map.get(key);
+ if (p.getNumber() != p0.getNumber())
+ return false;
+ }
+ return true;
+ }
+}
\ No newline at end of file
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex9/FullName.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex9/FullName.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex9/FullName.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex9/FullName.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.m2mmapex9;
+
+import javax.persistence.Embeddable;
+
+@Embeddable
+public class FullName {
+
+ String fName1;
+ String lName1;
+
+ public FullName() {}
+
+ public FullName(String fName, String lName) {
+ this.fName1 = fName;
+ this.lName1 = lName;
+ }
+
+ public String getFName() {
+ return fName1;
+ }
+
+ public void setFName(String fName) {
+ this.fName1 = fName;
+ }
+
+ public String getLName() {
+ return lName1;
+ }
+
+ public void setLName(String lName) {
+ this.lName1 = lName;
+ }
+
+ public boolean equals(Object o) {
+ if (!(o instanceof FullName))
+ return false;
+ FullName other = (FullName) o;
+ if (fName1.equals(other.fName1) &&
+ lName1.equals(other.lName1))
+ return true;
+ return false;
+ }
+
+ public int hashCode() {
+ int ret = 0;
+ ret += lName1.hashCode();
+ ret = 31 * ret + fName1.hashCode();
+ return ret;
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex9/FullPhoneName.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex9/FullPhoneName.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex9/FullPhoneName.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex9/FullPhoneName.java Thu Mar 5 17:30:20 2009
@@ -0,0 +1,67 @@
+/*
+ * 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.m2mmapex9;
+
+import javax.persistence.Embeddable;
+
+@Embeddable
+public class FullPhoneName {
+
+ String fName;
+ String lName;
+
+ public FullPhoneName() {}
+
+ public FullPhoneName(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 FullPhoneName)) return false;
+ FullPhoneName other = (FullPhoneName) 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/m2mmapex9/PhoneNumber.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex9/PhoneNumber.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex9/PhoneNumber.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex9/PhoneNumber.java Thu Mar 5 17:30:20 2009
@@ -0,0 +1,73 @@
+/*
+ * 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.m2mmapex9;
+
+import javax.persistence.*;
+
+import java.util.*;
+
+@Entity
+@Table(name="MEx9Phone")
+public class PhoneNumber {
+ @Id int number;
+
+ @ManyToMany(mappedBy="phones")
+ //@AttributeOverrides({
+ // @AttributeOverride(name="fName", column=@Column(name="fName_phone")),
+ // @AttributeOverride(name="lName", column=@Column(name="lName_phone"))
+ //})
+ Map<FullName, Employee> emps = new HashMap<FullName, Employee>();
+
+ public int getNumber() {
+ return number;
+ }
+
+ public void setNumber(int number) {
+ this.number = number;
+ }
+
+ public Map<FullName, Employee> getEmployees() {
+ return emps;
+ }
+
+ public void addEmployees(FullName d, Employee employee) {
+ emps.put(d, employee);
+ }
+
+ public void removeEmployee(FullName d) {
+ emps.remove(d);
+ }
+
+ public boolean equals(Object o) {
+ PhoneNumber p = (PhoneNumber) o;
+ Map<FullName, Employee> map = p.getEmployees();
+ if (p.getEmployees().size() != emps.size())
+ return false;
+ Collection<Map.Entry<FullName, Employee>> entries =
+ (Collection<Map.Entry<FullName, Employee>>) emps.entrySet();
+ for (Map.Entry<FullName, Employee> entry : entries) {
+ FullName key = entry.getKey();
+ Employee e = map.get(key);
+ Employee e0 = entry.getValue();
+ if (e.getEmpId() != e0.getEmpId())
+ return false;
+ }
+ return true;
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex9/TestMany2ManyMapEx9.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex9/TestMany2ManyMapEx9.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex9/TestMany2ManyMapEx9.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/m2mmapex9/TestMany2ManyMapEx9.java Thu Mar 5 17:30:20 2009
@@ -0,0 +1,247 @@
+/*
+ * 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.m2mmapex9;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+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 TestMany2ManyMapEx9 extends SingleEMFTestCase {
+
+ public int numEmployees = 2;
+ public int numPhoneNumbers = numEmployees + 1;
+ public int numEmployeesPerPhoneNumber = 2;
+ public int numPhoneNumbersPerEmployee = 2;
+ public Map<Integer, PhoneNumber> phones =
+ new HashMap<Integer, PhoneNumber>();
+ public List<String> namedQueries = new ArrayList<String>();
+
+ public int empId = 1;
+ public int phoneId = 1;
+ public int divId = 1;
+ public int deptId = 1;
+
+ public Map<Integer, Employee> empMap = new HashMap<Integer, Employee>();
+ public Map<Integer, PhoneNumber> phoneMap =
+ new HashMap<Integer, PhoneNumber>();
+
+ protected List<String> sql = new ArrayList<String>();
+ protected int sqlCount;
+
+ public void setUp() {
+ super.setUp(CLEAR_TABLES,
+ Employee.class,
+ FullName.class,
+ PhoneNumber.class,
+ FullPhoneName.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 PhoneNumber p, " +
+ " in (p.emps) e order by e.empId";
+ List rs = em.createQuery(query).getResultList();
+ FullName d = (FullName) rs.get(0);
+
+ String query2 = "select KEY(p) from Employee e, " +
+ " in (e.phones) p";
+ List rs2 = em.createQuery(query2).getResultList();
+ FullPhoneName k = (FullPhoneName) rs2.get(0);
+
+ em.clear();
+ String query4 = "select ENTRY(e) from PhoneNumber p, " +
+ " in (p.emps) e order by 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 < numEmployees; i++) {
+ Employee e = createEmployee(em, empId++);
+ empMap.put(e.getEmpId(), e);
+ }
+ tran.begin();
+ em.flush();
+ tran.commit();
+ em.close();
+ }
+
+ public Employee createEmployee(EntityManager em, int id) {
+ Employee e = new Employee();
+ e.setEmpId(id);
+ for (int i = 0; i < numPhoneNumbersPerEmployee; i++) {
+ FullPhoneName name1 = new FullPhoneName("f1" + id + i,
+ "l1" + id + i);
+ PhoneNumber phoneNumber = new PhoneNumber();
+ phoneNumber.setNumber(phoneId++);
+ FullName name2 = new FullName("f2" + phoneNumber.getNumber() + i,
+ "l2" + phoneNumber.getNumber() + i);
+ phoneNumber.addEmployees(name2, e);
+ e.addPhoneNumber(name1, phoneNumber);
+ em.persist(phoneNumber);
+ phoneMap.put(phoneNumber.getNumber(), phoneNumber);
+ }
+ em.persist(e);
+ return e;
+ }
+
+ public void removeAll() {
+ EntityManager em = emf.createEntityManager();
+ EntityTransaction tran = em.getTransaction();
+ tran.begin();
+ Query q = em.createNativeQuery("delete from phonenumber");
+ q.executeUpdate();
+ q = em.createNativeQuery("delete from employee");
+ q.executeUpdate();
+ System.out.println("committing removes");
+ tran.commit();
+ em.close();
+ }
+
+ public void findObj() throws Exception {
+ EntityManager em = emf.createEntityManager();
+ Employee e = em.find(Employee.class, 1);
+ assertEmployee(e);
+
+ PhoneNumber p = em.find(PhoneNumber.class, 1);
+ assertPhoneNumber(p);
+ em.close();
+ }
+
+ public void queryObj() throws Exception {
+ queryEmployee();
+ queryPhoneNumber();
+ }
+
+ public void queryPhoneNumber() throws Exception {
+ EntityManager em = emf.createEntityManager();
+ EntityTransaction tran = em.getTransaction();
+ tran.begin();
+ Query q = em.createQuery("select p from PhoneNumber p");
+ List<PhoneNumber> ps = q.getResultList();
+ for (PhoneNumber p : ps) {
+ assertPhoneNumber(p);
+ }
+ tran.commit();
+ em.close();
+ }
+
+ public void queryEmployee() throws Exception {
+ 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) throws Exception {
+ int id = e.getEmpId();
+ Employee e0 = empMap.get(id);
+ Map<FullPhoneName, PhoneNumber> phones = e.getPhoneNumbers();
+ Map<FullPhoneName, PhoneNumber> phones0 = e0.getPhoneNumbers();
+ Assert.assertEquals(phones0.size(), phones.size());
+ checkPhoneMap(phones0, phones);
+ }
+
+ public void assertPhoneNumber(PhoneNumber p) throws Exception {
+ int number = p.getNumber();
+ PhoneNumber p0 = phoneMap.get(number);
+ Map<FullName, Employee> es = p.getEmployees();
+ Map<FullName, Employee> es0 = p0.getEmployees();
+ Assert.assertEquals(es0.size(), es.size());
+ checkEmpMap(es0, es);
+ }
+
+ public void checkPhoneMap(Map<FullPhoneName, PhoneNumber> es0,
+ Map<FullPhoneName, PhoneNumber> es) throws Exception {
+ Collection<Map.Entry<FullPhoneName, PhoneNumber>> entrySets0 =
+ es0.entrySet();
+ for (Map.Entry<FullPhoneName, PhoneNumber> entry0 : entrySets0) {
+ FullPhoneName key0 = entry0.getKey();
+ PhoneNumber p0 = entry0.getValue();
+ PhoneNumber p = es.get(key0);
+ if (!p0.equals(p))
+ throw new Exception("Assertion Failure");
+ }
+ }
+
+ public void checkEmpMap(Map<FullName, Employee> es0,
+ Map<FullName, Employee> es) throws Exception {
+ Collection<Map.Entry<FullName, Employee>> entrySets0 = es0.entrySet();
+ for (Map.Entry<FullName, Employee> entry0 : entrySets0) {
+ FullName key0 = entry0.getKey();
+ Employee e0 = entry0.getValue();
+ Employee e = es.get(key0);
+ if (!e0.equals(e))
+ throw new Exception("Assertion failure");
+ }
+ }
+
+ 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_ex0/Department1.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/Department1.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/Department1.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/Department1.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_ex0;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.persistence.*;
+
+@Entity
+@Table(name="S26Dept1")
+//@Access(AccessType.PROPERTY)
+public class Department1 {
+
+ //@Id
+ int deptId;
+
+ //@OneToMany(mappedBy="department", fetch=FetchType.EAGER)
+ //@MapKey(name="empId")
+ Map<Integer, Employee1> empMap = new HashMap<Integer, Employee1>();
+
+ @Id
+ public int getDeptId() {
+ return deptId;
+ }
+
+ public void setDeptId(int deptId) {
+ this.deptId = deptId;
+ }
+
+ @OneToMany(mappedBy="department", fetch=FetchType.EAGER)
+ @MapKey(name="empId")
+ public Map<Integer, Employee1> getEmpMap() {
+ return empMap;
+ }
+
+ public void setEmpMap(Map<Integer, Employee1> empMap) {
+ this.empMap = empMap;
+ }
+
+ //public void addEmployee(Employee emp) {
+ // empMap.put(emp.getEmpId(), emp);
+ //}
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/Department2.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/Department2.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/Department2.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/Department2.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_ex0;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.persistence.*;
+
+@Entity
+@Table(name="S26Dept2")
+public class Department2 {
+
+ int deptId;
+ Map<EmployeePK2, Employee2> empMap = new HashMap<EmployeePK2, Employee2>();
+
+ @Id
+ public int getDeptId() {
+ return deptId;
+ }
+
+ public void setDeptId(int deptId) {
+ this.deptId = deptId;
+ }
+
+ @OneToMany(mappedBy="department")
+ @MapKey(name="empPK")
+ public Map<EmployeePK2, Employee2> getEmpMap() {
+ return empMap;
+ }
+
+ public void setEmpMap(Map<EmployeePK2, Employee2> empMap) {
+ this.empMap = empMap;
+ }
+
+ public void addEmployee(Employee2 emp) {
+ empMap.put(emp.getEmpPK(), emp);
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/Department3.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/Department3.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/Department3.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/Department3.java Thu Mar 5 17:30:20 2009
@@ -0,0 +1,58 @@
+/*
+ * 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.HashMap;
+import java.util.Map;
+
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.Id;
+import javax.persistence.MapKey;
+import javax.persistence.OneToMany;
+import javax.persistence.Table;
+
+@Entity
+@Table(name="S26Dept3")
+public class Department3 {
+
+ @Id
+ int deptId;
+
+ @OneToMany(mappedBy="department", fetch=FetchType.EAGER)
+ @MapKey(name="name")
+ Map<EmployeeName3, Employee3> emps =
+ new HashMap<EmployeeName3, Employee3>();
+
+ public int getDeptId() {
+ return deptId;
+ }
+
+ public void setDeptId(int deptId) {
+ this.deptId = deptId;
+ }
+
+ public Map<EmployeeName3, Employee3> getEmployees() {
+ return emps;
+ }
+
+ public void addEmployee(Employee3 emp) {
+ emps.put(emp.getName(), emp);
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/Employee1.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/Employee1.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/Employee1.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/Employee1.java Thu Mar 5 17:30:20 2009
@@ -0,0 +1,51 @@
+/*
+ * 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 javax.persistence.*;
+
+@Entity
+@Table(name="S26Emp1")
+public class Employee1 {
+ @Id
+ int empId;
+
+ @ManyToOne
+ @JoinColumn(name="dept_id")
+ Department1 department;
+
+ //@Id
+ public int getEmpId() {
+ return empId;
+ }
+
+ public void setEmpId(int empId) {
+ this.empId = empId;
+ }
+
+ //@ManyToOne
+ //@JoinColumn(name="dept_id")
+ public Department1 getDepartment() {
+ return department;
+ }
+
+ public void setDepartment(Department1 department) {
+ this.department = department;
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/Employee2.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/Employee2.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/Employee2.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/Employee2.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_ex0;
+
+import java.util.Date;
+
+import javax.persistence.*;
+
+@Entity
+@Table(name="S26Emp2")
+public class Employee2 {
+ EmployeePK2 empPK;
+
+ Department2 department;
+
+ public Employee2() {}
+
+ public Employee2(String name, Date bDate) {
+ this.empPK = new EmployeePK2(name, bDate);
+ }
+
+ @EmbeddedId
+ public EmployeePK2 getEmpPK() {
+ return empPK;
+ }
+
+ public void setEmpPK(EmployeePK2 empPK) {
+ this.empPK = empPK;
+ }
+
+ @ManyToOne
+ @JoinColumn(name="dept_id")
+ public Department2 getDepartment() {
+ return department;
+ }
+
+ public void setDepartment(Department2 department) {
+ this.department = department;
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/Employee3.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/Employee3.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/Employee3.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/Employee3.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_ex0;
+
+import javax.persistence.*;
+
+@Entity
+@Table(name="S26Emp3")
+public class Employee3 {
+ @Id
+ int empId;
+
+ @ManyToOne
+ @JoinColumn(name="dept_id")
+ Department3 department;
+
+ @Embedded
+ EmployeeName3 name;
+
+ public int getEmpId() {
+ return empId;
+ }
+
+ public void setEmpId(int empId) {
+ this.empId = empId;
+ }
+
+ public Department3 getDepartment() {
+ return department;
+ }
+
+ public void setDepartment(Department3 department) {
+ this.department = department;
+ }
+
+ public EmployeeName3 getName() {
+ return name;
+ }
+
+ public void setName(EmployeeName3 name) {
+ this.name = name;
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/EmployeeName3.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/EmployeeName3.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/EmployeeName3.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/EmployeeName3.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_ex0;
+
+import javax.persistence.Embeddable;
+
+@Embeddable
+public class EmployeeName3 {
+
+ String fName;
+ String lName;
+
+ public EmployeeName3() {}
+
+ public EmployeeName3(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 EmployeeName3))
+ return false;
+ EmployeeName3 other = (EmployeeName3) 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_ex0/EmployeePK2.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/EmployeePK2.java?rev=750517&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/EmployeePK2.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_26_ex0/EmployeePK2.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_ex0;
+
+import java.io.Serializable;
+import java.util.Date;
+
+import javax.persistence.*;
+
+@Embeddable
+public class EmployeePK2 implements Serializable {
+ String name;
+ Date bDay;
+
+ public EmployeePK2() {}
+ public EmployeePK2(String name, Date bDay) {
+ this.name = name;
+ this.bDay = bDay;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o)
+ return true;
+ if (!(o instanceof EmployeePK2))
+ return false;
+ EmployeePK2 pk = (EmployeePK2) 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;
+ }
+}