From 6bbbb786c20890f2d4d54bca238690651e177758 Mon Sep 17 00:00:00 2001 From: Fay Wang Date: Fri, 6 Feb 2009 20:11:54 +0000 Subject: [PATCH] OPENJPA-871: check in MappedById test case git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@741701 13f79535-47bb-0310-9956-ffa450edef68 --- .../enhance/identity/Dependent1.java | 67 ++++++++++ .../enhance/identity/DependentId1.java | 68 ++++++++++ .../enhance/identity/Employee1.java | 88 ++++++++++++ .../enhance/identity/TestMappedById.java | 125 ++++++++++++++++++ 4 files changed, 348 insertions(+) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent1.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId1.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee1.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMappedById.java diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent1.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent1.java new file mode 100644 index 000000000..991dd3d94 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent1.java @@ -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.enhance.identity; + +import javax.persistence.*; + + +@Entity +public class Dependent1 { + @EmbeddedId + DependentId1 id; + + @MappedById("empPK") + @ManyToOne Employee1 emp; + + public Employee1 getEmp() { + return emp; + } + + public void setEmp(Employee1 emp) { + this.emp = emp; + } + + public DependentId1 getId() { + return id; + } + + public void setId(DependentId1 id) { + this.id = id; + } + + public boolean equals(Object o) { + if (o == null) return false; + if (!(o instanceof Dependent1)) return false; + Dependent1 d0 = (Dependent1)o; + DependentId1 id0 = d0.getId(); + if (!id.equals(id0)) return false; + Employee1 e0 = d0.getEmp(); + if (emp != null && !emp.equals(e0)) return false; + if (emp == null && e0 != null) return false; + return true; + } + + public int hashCode() { + int ret = 0; + ret = ret * 31 + id.hashCode(); + ret = ret * 31 + emp.hashCode(); + return ret; + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId1.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId1.java new file mode 100644 index 000000000..67ef2d9f7 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId1.java @@ -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.enhance.identity; + +import javax.persistence.*; + +@Embeddable +public class DependentId1 { + String name; + long empPK; + + public DependentId1() {} + + public DependentId1(String name, long empPK) { + this.name = name; + this.empPK = empPK; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setEmpPK(long empPK) { + this.empPK = empPK; + } + + public long getEmpPK() { + return empPK; + } + + public boolean equals(Object o) { + if (o == null) return false; + if (!(o instanceof DependentId1)) return false; + DependentId1 d = (DependentId1)o; + if (empPK != d.getEmpPK()) return false; + if (name != null && !name.equals(d.getName())) return false; + if (name == null && d.getName() != null) return false; + return true; + } + + public int hashCode() { + int ret = 0; + ret = (int) (ret * 31 + empPK); + ret = ret * 31 + name.hashCode(); + return ret; + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee1.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee1.java new file mode 100644 index 000000000..79b83d78b --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee1.java @@ -0,0 +1,88 @@ +/* + * 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.enhance.identity; + +import javax.persistence.*; +import java.util.*; + +@Entity +public class Employee1 { + @Id + int empId; + + String name; + + @OneToMany(mappedBy="emp") + List dependents = new ArrayList(); + + public int getEmpId() { + return empId; + } + + public void setEmpId(int empId) { + this.empId = empId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getDependents() { + return dependents; + } + + public void setDependents(List dependents) { + this.dependents = dependents; + } + + public void addDependent(Dependent1 d) { + dependents.add(d); + } + + public boolean equals(Object o) { + if (o == null) return false; + if (!(o instanceof Employee1)) return false; + Employee1 e = (Employee1)o; + if (empId != e.getEmpId()) return false; + if (name != null && !name.equals(e.getName())) return false; + if (name == null && e.getName() != null) return false; + List ds0 = e.getDependents(); + if (ds0 != null && ds0.size() != 0 && dependents == null) return false; + if (ds0 == null && dependents != null && dependents.size() != 0) return false; + if (ds0 == null && dependents == null) return true; + if (ds0 != null && dependents != null) { + if (ds0.size() != dependents.size()) return false; + } + return true; + } + + public int hashCode() { + int ret = 0; + ret = ret * 31 + empId; + ret = ret * 31 + name.hashCode(); + if (dependents != null) + for (Dependent1 d : dependents) + ret = ret * 31 + d.id.hashCode(); + return ret; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMappedById.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMappedById.java new file mode 100644 index 000000000..21fef0055 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMappedById.java @@ -0,0 +1,125 @@ +/* + * 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.enhance.identity; + +import java.util.ArrayList; +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 org.apache.openjpa.persistence.test.SingleEMFTestCase; + +public class TestMappedById extends SingleEMFTestCase { + public int numEmployees = 4; + public int numDependentsPerEmployee = 2; + public List namedQueries = new ArrayList(); + public Map emps = new HashMap(); + public Map deps = new HashMap(); + + public int eId = 1; + public int dId = 1; + + public void setUp() throws Exception { + super.setUp(CLEAR_TABLES, Dependent1.class, Employee1.class, + DependentId1.class); + } + + /** + * This is a spec 2.4.1.2 Example 1, case(b) + */ + public void testMappedById1() { + createObj(); + findObj(); + queryObj(); + } + + public void createObj() { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + for (int i = 0; i < numEmployees; i++) + createEmployee(em, eId++); + tran.begin(); + em.flush(); + tran.commit(); + em.close(); + } + + public Employee1 createEmployee(EntityManager em, int id) { + Employee1 e = new Employee1(); + e.setEmpId(id); + e.setName("emp_" + id); + for (int i = 0; i < numDependentsPerEmployee; i++) { + Dependent1 d = createDependent(em, dId++, e); + e.addDependent(d); + em.persist(d); + } + em.persist(e); + emps.put(id, e); + return e; + } + + public Dependent1 createDependent(EntityManager em, int id, Employee1 e) { + Dependent1 d = new Dependent1(); + DependentId1 did = new DependentId1(); + did.setName("dep_" + id); + d.setId(did); + d.setEmp(e); + deps.put(did.getName(), d); + return d; + } + + public void findObj() { + EntityManager em = emf.createEntityManager(); + Employee1 e = em.find(Employee1.class, 1); + List ds = e.getDependents(); + assertEquals(numDependentsPerEmployee, ds.size()); + Employee1 e0 = emps.get(1); + assertEquals(e0, e); + } + + public void queryObj() { + queryDependent(); + } + + public void queryDependent() { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + String jpql = "select d from Dependent1 d where d.id.name = 'dep_1' AND d.emp.name = 'emp_1'"; + Query q = em.createQuery(jpql); + List ds = q.getResultList(); + for (Dependent1 d : ds) { + assertDependent(d); + } + tran.commit(); + em.close(); + } + + public void assertDependent(Dependent1 d) { + DependentId1 id = d.getId(); + Dependent1 d0 = deps.get(id.getName()); + if (d0.id.empPK == 0) + d0.id.empPK = d0.emp.getEmpId(); + assertEquals(d0, d); + } +}