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/02/06 21:11:54 UTC

svn commit: r741701 - in /openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity: Dependent1.java DependentId1.java Employee1.java TestMappedById.java

Author: faywang
Date: Fri Feb  6 20:11:54 2009
New Revision: 741701

URL: http://svn.apache.org/viewvc?rev=741701&view=rev
Log:
OPENJPA-871: check in MappedById test case

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent1.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId1.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee1.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMappedById.java

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent1.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent1.java?rev=741701&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent1.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent1.java Fri Feb  6 20:11:54 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.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;
+    }
+    
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId1.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId1.java?rev=741701&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId1.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId1.java Fri Feb  6 20:11:54 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.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;
+    }
+    
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee1.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee1.java?rev=741701&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee1.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee1.java Fri Feb  6 20:11:54 2009
@@ -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<Dependent1> dependents = new ArrayList<Dependent1>();
+    
+    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<Dependent1> getDependents() {
+        return dependents;
+    }
+    
+    public void setDependents(List<Dependent1> 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<Dependent1> 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;
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMappedById.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMappedById.java?rev=741701&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMappedById.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMappedById.java Fri Feb  6 20:11:54 2009
@@ -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<String> namedQueries = new ArrayList<String>();
+    public Map<Integer, Employee1> emps = new HashMap<Integer, Employee1>();
+    public Map<String, Dependent1> deps = new HashMap<String, Dependent1>();
+
+    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<Dependent1> 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<Dependent1> 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);
+    }
+}