OPENJPA-871: check in test case for multiple MappedById

annotations with generated key

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@745945 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fay Wang 2009-02-19 17:48:28 +00:00
parent 679989fd16
commit 40ec58ce0d
5 changed files with 428 additions and 2 deletions

View File

@ -0,0 +1,82 @@
/*
* 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
@Table(name="DEP3_MBI")
public class Dependent3 {
@EmbeddedId
DependentId3 id;
@MappedById("empPK")
@ManyToOne Employee3 emp;
@MappedById("parentPK")
@OneToOne Parent3 parent;
public Employee3 getEmp() {
return emp;
}
public void setEmp(Employee3 emp) {
this.emp = emp;
}
public Parent3 getParent() {
return parent;
}
public void setParent(Parent3 parent) {
this.parent = parent;
}
public DependentId3 getId() {
return id;
}
public void setId(DependentId3 id) {
this.id = id;
}
public boolean equals(Object o) {
if (o == null) return false;
if (!(o instanceof Dependent3)) return false;
Dependent3 d0 = (Dependent3)o;
DependentId3 id0 = d0.getId();
if (!id.equals(id0)) return false;
Employee3 e0 = d0.getEmp();
if (emp != null && !emp.equals(e0)) return false;
if (emp == null && e0 != null) return false;
Parent3 p0 = d0.getParent();
if (parent != null && !parent.equals(p0)) return false;
if (parent == null && p0 != null) return false;
return true;
}
public int hashCode() {
int ret = 0;
ret = ret * 31 + id.hashCode();
ret = ret * 31 + emp.hashCode();
ret = ret * 31 + parent.hashCode();
return ret;
}
}

View File

@ -0,0 +1,81 @@
/*
* 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 DependentId3 {
String name;
long empPK;
long parentPK;
public DependentId3() {}
public DependentId3(String name, long empPK,
long parentPK) {
this.name = name;
this.empPK = empPK;
this.parentPK = parentPK;
}
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 void setParentPK(long parentPK) {
this.parentPK = parentPK;
}
public long getParentPK() {
return parentPK;
}
public boolean equals(Object o) {
if (o == null) return false;
if (!(o instanceof DependentId3)) return false;
DependentId3 d = (DependentId3)o;
if (empPK != d.getEmpPK()) return false;
if (parentPK != d.getParentPK()) 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 = (int) (ret * 31 + parentPK);
ret = ret * 31 + name.hashCode();
return ret;
}
}

View File

@ -0,0 +1,87 @@
/*
* 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
@Table(name="EMP2_MBI")
public class Employee3 {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
int empId;
String name;
@OneToMany(mappedBy="emp")
List<Dependent3> dependents = new ArrayList<Dependent3>();
public int getEmpId() {
return empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Dependent3> getDependents() {
return dependents;
}
public void setDependents(List<Dependent3> dependents) {
this.dependents = dependents;
}
public void addDependent(Dependent3 d) {
dependents.add(d);
}
public boolean equals(Object o) {
if (o == null) return false;
if (!(o instanceof Employee3)) return false;
Employee3 e = (Employee3)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<Dependent3> 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 (Dependent3 d : dependents)
ret = ret * 31 + d.id.hashCode();
return ret;
}
}

View File

@ -0,0 +1,76 @@
/*
* 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
@Table(name="PRT2_MBI")
public class Parent3 {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
int pid;
String name;
@OneToOne(mappedBy="parent")
Dependent3 dependent;
public int getPid() {
return pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dependent3 getDependent() {
return dependent;
}
public void setDependent(Dependent3 dependent) {
this.dependent = dependent;
}
public boolean equals(Object o) {
if (o == null) return false;
if (!(o instanceof Parent3)) return false;
Parent3 e = (Parent3)o;
if (pid != e.getPid()) return false;
if (name != null && !name.equals(e.getName())) return false;
if (name == null && e.getName() != null) return false;
Dependent3 d0 = e.getDependent();
if (!dependent.id.equals(d0.id)) return false;
return true;
}
public int hashCode() {
int ret = 0;
ret = ret * 31 + pid;
ret = ret * 31 + name.hashCode();
if (dependent != null)
ret = ret * 31 + dependent.id.hashCode();
return ret;
}
}

View File

@ -18,6 +18,7 @@
*/
package org.apache.openjpa.persistence.enhance.identity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -28,7 +29,10 @@ import javax.persistence.Query;
import junit.framework.Assert;
import org.apache.openjpa.enhance.PersistenceCapable;
import org.apache.openjpa.kernel.StateManagerImpl;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
import org.apache.openjpa.util.ObjectId;
public class TestMappedById extends SingleEMFTestCase {
public int numEmployees = 4;
@ -46,10 +50,18 @@ public class TestMappedById extends SingleEMFTestCase {
public Map<String, MedicalHistory2> medicals2 =
new HashMap<String, MedicalHistory2>();
public Map<Integer, Employee3> emps3 = new HashMap<Integer, Employee3>();
public Map<Object, Dependent3> depMap3 =
new HashMap<Object, Dependent3>();
public List dids3 = new ArrayList();
public List<Dependent3> deps3 = new ArrayList<Dependent3>();
public int eId1 = 1;
public int dId1 = 1;
public int eId2 = 1;
public int dId2 = 1;
public int eId3 = 1;
public int dId3 = 1;
public int pId1 = 1;
public int mId1 = 1;
public int pId2 = 1;
@ -60,7 +72,8 @@ public class TestMappedById extends SingleEMFTestCase {
DependentId1.class, Dependent2.class, Employee2.class,
DependentId2.class, EmployeeId2.class, MedicalHistory1.class,
Person1.class, PersonId1.class, MedicalHistory2.class,
Person2.class);
Person2.class, Dependent3.class, Employee3.class,
DependentId3.class, Parent3.class);
}
/**
@ -91,13 +104,24 @@ public class TestMappedById extends SingleEMFTestCase {
}
/**
* This is spec 2.4.1.2 Example 4, case(b) with generated key
* This is a variation of spec 2.4.1.2 Example 4, case(b) with generated key
*/
public void testMappedById4() {
createObj4();
queryObj4();
}
/**
* This is a variation of spec 2.4.1.2 Example 1, case(b):
* two MappedById annotations in Dependent3 and both parent
* classes use generated key
*/
public void testMappedById5() {
createObj5();
findObj5();
queryObj5();
}
public void createObj1() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
@ -397,4 +421,80 @@ public class TestMappedById extends SingleEMFTestCase {
Assert.assertEquals(m1, m);
}
public void createObj5() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
for (int i = 0; i < numEmployees; i++)
createEmployee3(em, eId3++);
tran.begin();
em.flush();
tran.commit();
for (Dependent3 d: deps3) {
ObjectId did = (ObjectId)((StateManagerImpl)((PersistenceCapable)d).pcGetStateManager()).getObjectId();
dids3.add(did.getId());
depMap3.put(did.getId(), d);
}
em.close();
}
public Employee3 createEmployee3(EntityManager em, int id) {
Employee3 e = new Employee3();
e.setName("emp_" + id);
for (int i = 0; i < numDependentsPerEmployee; i++) {
Dependent3 d = createDependent3(em, dId3++, e);
e.addDependent(d);
em.persist(d);
}
em.persist(e);
emps3.put(id, e);
return e;
}
public Dependent3 createDependent3(EntityManager em, int id, Employee3 e) {
Dependent3 d = new Dependent3();
DependentId3 did = new DependentId3();
did.setName("dep_" + id);
d.setId(did);
d.setEmp(e);
deps3.add(d);
Parent3 p = new Parent3();
p.setName("p_" + id);
p.setDependent(d);
d.setParent(p);
em.persist(p);
return d;
}
public void findObj5() {
EntityManager em = emf.createEntityManager();
Dependent3 d = em.find(Dependent3.class, dids3.get(1));
Dependent3 d0 = depMap3.get(dids3.get(1));
assertEquals(d0, d);
}
public void queryObj5() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
String jpql = "select d from Dependent3 d where d.id.name = 'dep_1' AND d.emp.name = 'emp_1'";
Query q = em.createQuery(jpql);
List<Dependent3> ds = q.getResultList();
for (Dependent3 d : ds) {
assertDependent3(d);
}
tran.commit();
em.close();
}
public void assertDependent3(Dependent3 d) {
DependentId3 id = d.getId();
Dependent3 d0 = depMap3.get(id);
if (d0.id.empPK == 0)
d0.id.empPK = d0.emp.getEmpId();
assertEquals(d0, d);
}
}