OPENJPA-931: support nesting EmbeddedId in IdClass

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@900231 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fay Wang 2010-01-17 22:39:32 +00:00
parent dff9c690bc
commit d31639d7b9
6 changed files with 246 additions and 1 deletions

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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();