From d31639d7b96d0d2c7a83172d996eb52757406b97 Mon Sep 17 00:00:00 2001 From: Fay Wang Date: Sun, 17 Jan 2010 22:39:32 +0000 Subject: [PATCH] OPENJPA-931: support nesting EmbeddedId in IdClass git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@900231 13f79535-47bb-0310-9956-ffa450edef68 --- .../meta/strats/RelationFieldStrategy.java | 7 ++ .../enhance/identity/Dependent5.java | 49 +++++++++++++ .../enhance/identity/DependentId5.java | 60 ++++++++++++++++ .../enhance/identity/Employee5.java | 40 +++++++++++ .../enhance/identity/EmployeeId5.java | 70 +++++++++++++++++++ .../enhance/identity/TestMappedById.java | 21 +++++- 6 files changed, 246 insertions(+), 1 deletion(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent5.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId5.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee5.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/EmployeeId5.java diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java index 95399c609..1d6f1f4ad 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java @@ -37,6 +37,7 @@ import org.apache.openjpa.jdbc.meta.FieldMapping; import org.apache.openjpa.jdbc.meta.FieldStrategy; import org.apache.openjpa.jdbc.meta.Joinable; import org.apache.openjpa.jdbc.meta.MappingInfo; +import org.apache.openjpa.jdbc.meta.ValueHandler; import org.apache.openjpa.jdbc.meta.ValueMapping; import org.apache.openjpa.jdbc.meta.ValueMappingImpl; import org.apache.openjpa.jdbc.meta.ValueMappingInfo; @@ -65,6 +66,7 @@ import org.apache.openjpa.util.ApplicationIds; import org.apache.openjpa.util.ImplHelper; import org.apache.openjpa.util.InternalException; import org.apache.openjpa.util.MetaDataException; +import org.apache.openjpa.util.ObjectId; import org.apache.openjpa.util.OpenJPAId; import org.apache.openjpa.util.UnsupportedException; @@ -994,6 +996,8 @@ public class RelationFieldStrategy col = field.getForeignKey().getPrimaryKeyColumn(col); if (col == null) throw new InternalException(); + + Object savedFieldVal = fieldVal; ClassMapping relmapping = field.getTypeMapping(); Joinable j = field.getTypeMapping().assertJoinable(col); @@ -1006,6 +1010,9 @@ public class RelationFieldStrategy Object[] pks = ApplicationIds.toPKValues(fieldVal, relmapping); fieldVal = pks[relmapping.getField(j.getFieldIndex()). getPrimaryKeyIndex()]; + } else if (relmapping.getObjectIdType() == ObjectId.class && + relmapping.getPrimaryKeyFieldMappings()[0].getValueMapping().isEmbedded()) { + return j.getJoinValue(savedFieldVal, col, store); } return j.getJoinValue(fieldVal, col, store); } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent5.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent5.java new file mode 100644 index 000000000..a8d8f0a43 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent5.java @@ -0,0 +1,49 @@ +/* + * 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.io.Serializable; + +import javax.persistence.*; + +@Entity +@IdClass(DependentId5.class) +public class Dependent5 implements Serializable { + + private static final long serialVersionUID = 1L; + @Id + String name; + + @Id + @JoinColumns({ + @JoinColumn(name="FIRSTNAME", referencedColumnName="FIRSTNAME"), + @JoinColumn(name="LASTNAME", referencedColumnName="LASTNAME") + }) + @ManyToOne + Employee5 emp; + + public Dependent5(String name, Employee5 emp) { + this.name = name; + this.emp = emp; + } + + public Dependent5(DependentId5 dId, Employee5 emp){ + this.name = dId.getName(); + this.emp = emp; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId5.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId5.java new file mode 100644 index 000000000..2fa0658d0 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId5.java @@ -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.enhance.identity; + +public class DependentId5 { + String name; + EmployeeId5 emp; + + public DependentId5() { + } + + public DependentId5(String name, EmployeeId5 emp) { + this.name = name; + this.emp = emp; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public EmployeeId5 getEmp() { + return emp; + } + + public void setEmp(EmployeeId5 emp) { + this.emp = emp; + } + + public int hashCode() { + return name.hashCode() + emp.hashCode(); + } + + public boolean equals(Object o) { + if (!(o instanceof DependentId5)) return false; + DependentId5 d = (DependentId5) o; + if (!emp.equals(d.emp)) return false; + if (!name.equals(d.name)) return false; + return true; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee5.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee5.java new file mode 100644 index 000000000..d8ea7c1a5 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee5.java @@ -0,0 +1,40 @@ +/* + * 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.io.*; + +@Entity +public class Employee5 implements Serializable { + + private static final long serialVersionUID = 1L; + + @EmbeddedId + EmployeeId5 empId; + + public Employee5() { + } + + public Employee5(EmployeeId5 eId) { + this.empId=eId; + } + + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/EmployeeId5.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/EmployeeId5.java new file mode 100644 index 000000000..3e0799eb4 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/EmployeeId5.java @@ -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.enhance.identity; + +import javax.persistence.Embeddable; + +@Embeddable +public class EmployeeId5 implements java.io.Serializable { + + String firstName; + String lastName; + + public EmployeeId5() { + } + + public EmployeeId5(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public boolean equals(Object o) { + if (!(o instanceof EmployeeId5)) + return false; + EmployeeId5 other = (EmployeeId5) o; + if (firstName.equals(other.firstName) && + lastName.equals(other.lastName)) + return true; + return false; + } + + public int hashCode() { + int ret = 0; + ret += firstName.hashCode(); + ret = 31 * ret + lastName.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 index 4e299d91f..844ab6e13 100644 --- 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 @@ -87,7 +87,8 @@ public class TestMappedById extends SingleEMFTestCase { Person4.class, PersonId4.class, MedicalHistory4.class, Dependent3.class, Employee3.class, DependentId3.class, Parent3.class, Dependent4.class, Employee4.class, PhoneNumber.class, - BeneContact.class, BeneContactId.class, Beneficiary.class); + BeneContact.class, BeneContactId.class, Beneficiary.class, + Dependent5.class, Employee5.class, EmployeeId5.class); } /** @@ -186,6 +187,24 @@ public class TestMappedById extends SingleEMFTestCase { em.close(); } + public void testEmbeddedIdContainedInIdClass() { + EntityManager em = emf.createEntityManager(); + EmployeeId5 eId1 = new EmployeeId5("Java", "Duke"); + Employee5 employee1 = new Employee5(eId1); + Dependent5 dep1 = new Dependent5("1", employee1); + + em.persist(dep1); + em.persist(employee1); + + em.getTransaction().begin(); + em.flush(); + em.getTransaction().commit(); + em.clear(); + + DependentId5 depId1 = new DependentId5("1", eId1); + Dependent5 newDep = em.find(Dependent5.class, depId1); + assertNotNull(newDep); + } public void createObj1() { EntityManager em = emf.createEntityManager();