diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedValueHandler.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedValueHandler.java index 2887a61b6..154d55c90 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedValueHandler.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedValueHandler.java @@ -247,8 +247,11 @@ public abstract class EmbedValueHandler embed.loadEmbedded(em, store, fetch, cval); else { if (!(em instanceof ObjectIdStateManager)) - cval = embed.toEmbeddedObjectValue(cval); - em.store(fms[i].getIndex(), cval); + cval = embed.toEmbeddedObjectValue(cval); + if (fms[i].getHandler() != null) + cval = fms[i].getHandler().toObjectValue(fms[i], cval); + + em.store(fms[i].getIndex(), cval); } } return idx; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/BeneContact.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/BeneContact.java new file mode 100644 index 000000000..2708c425b --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/BeneContact.java @@ -0,0 +1,109 @@ +/* + * 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.Date; + +import javax.persistence.Column; +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.MappedById; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.persistence.Version; + + +@Entity +@Table(name="BENE_CONTACT") +public class BeneContact { + + private static final long serialVersionUID = 4571838649566012594L; + + private BeneContactId id; + + private Beneficiary beneficiary; + + private String email; + + private String phone; + + private Date lastUpdateDate; + + private int version; + + @Version + public int getVersion() { + return version; + } + + public void setVersion(int version) { + this.version = version; + } + + @Temporal(TemporalType.TIMESTAMP) + public Date getLastUpdateDate() { + return lastUpdateDate; + } + + public void setLastUpdateDate(Date lastUpdateDate) { + this.lastUpdateDate = lastUpdateDate; + } + + @Column(name="EMAIL") + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + @Column(name="PHONE") + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + @EmbeddedId + public BeneContactId getId() { + return id; + } + + public void setId(BeneContactId id) { + this.id = id; + } + + @ManyToOne(targetEntity=Beneficiary.class, fetch=FetchType.EAGER) + @JoinColumn(name="ID") + @MappedById("beneficiaryPK") + public Beneficiary getBeneficiary() { + return beneficiary; + } + + public void setBeneficiary(Beneficiary beneficiary) { + this.beneficiary = beneficiary; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/BeneContactId.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/BeneContactId.java new file mode 100644 index 000000000..e29ba4f17 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/BeneContactId.java @@ -0,0 +1,74 @@ +/* + * 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.Column; +import javax.persistence.Embeddable; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; + +@Embeddable +public class BeneContactId implements Serializable { + private static final long serialVersionUID = -837443719842439462L; + ContactType type; + + String beneficiaryPK; + + public boolean equals(Object obj) { + if(this == obj) return true; + if((obj != null) && (obj instanceof BeneContactId)) { + BeneContactId other = (BeneContactId) obj; + if(this.type.equals(other.type) && this.beneficiaryPK.equals(other.beneficiaryPK)) + return true; + } + return false; + } + + public int hashCode() { + String hash = beneficiaryPK + Integer.toString(type.ordinal()); + return hash.hashCode(); + } + + public String toString() { + return type.toString() + "-" + beneficiaryPK; + } + + @Column(name="ID") + public String getBeneficiaryKey() { + return beneficiaryPK; + } + + public void setBeneficiaryKey(String id) { + beneficiaryPK = id; + } + + @Enumerated(EnumType.STRING) + @Column(name="TYPE") + public ContactType getContactType() { + return type; + } + + public void setContactType(ContactType type) { + this.type = type; + } + + public enum ContactType {HOME, BUSINESS, OTHER;} +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Beneficiary.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Beneficiary.java new file mode 100644 index 000000000..64be2ec2a --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Beneficiary.java @@ -0,0 +1,100 @@ +/* + * 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.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.OneToMany; +import javax.persistence.Table; + + +@Entity +@Table(name="BENEFICIARY") +public class Beneficiary { + + private static final long serialVersionUID = -452903666159175508L; + + private String annuityHolderId; + + private String firstName; + + private String lastName; + + private String relationship; + + private List contacts; + + private String id; + + @javax.persistence.Id + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + @Column(name="FK_ANNUITY_HOLDER_ID") + public String getAnnuityHolderId() { + return annuityHolderId; + } + + public void setAnnuityHolderId(String annuityHolderId) { + this.annuityHolderId = annuityHolderId; + } + + @Column(name="FIRST_NAME") + public String getFirstName() { + return firstName; + } + + public void setFirstName(String first) { + this.firstName = first; + } + + @Column(name="LAST_NAME") + public String getLastName() { + return lastName; + } + public void setLastName(String last) { + this.lastName = last; + } + + @Column(name="RELATIONSHIP") + public String getRelationship() { + return relationship; + } + + public void setRelationship(String relationship) { + this.relationship = relationship; + } + + @OneToMany(targetEntity=BeneContact.class, mappedBy="beneficiary", fetch=FetchType.EAGER) + public List getContacts() { + return contacts; + } + + public void setContacts(List contacts) { + this.contacts = contacts; + } +} 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 0b65fd277..55c8c065f 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 @@ -86,7 +86,8 @@ public class TestMappedById extends SingleEMFTestCase { Person2.class, Person3.class, MedicalHistory3.class, Person4.class, PersonId4.class, MedicalHistory4.class, Dependent3.class, Employee3.class, DependentId3.class, - Parent3.class, Dependent4.class, Employee4.class); + Parent3.class, Dependent4.class, Employee4.class, + BeneContact.class, BeneContactId.class, Beneficiary.class); } /** @@ -737,4 +738,29 @@ public class TestMappedById extends SingleEMFTestCase { } return e; } + + public void testEnumInEmbeddedId() { + EntityManager em = emf.createEntityManager(); + Beneficiary b = new Beneficiary(); + b.setId("b8"); + List contacts = new ArrayList(); + BeneContact c = new BeneContact(); + c.setEmail("email8"); + BeneContactId id = new BeneContactId(); + id.setContactType(BeneContactId.ContactType.HOME); + c.setBeneficiary(b); + + c.setId(id); + em.persist(c); + contacts.add(c); + b.setContacts(contacts); + em.persist(b); + em.getTransaction().begin(); + em.flush(); + em.getTransaction().commit(); + em.clear(); + BeneContactId id1 = c.getId(); + BeneContact c1 = em.find(BeneContact.class, id1); + assertEquals("email8", c1.getEmail()); + } }