OPENJPA-1204: fix enum evaluation in EmbeddedId

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@798868 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fay Wang 2009-07-29 12:09:16 +00:00
parent 1b128ce338
commit ec483537da
5 changed files with 315 additions and 3 deletions

View File

@ -247,8 +247,11 @@ public abstract class EmbedValueHandler
embed.loadEmbedded(em, store, fetch, cval); embed.loadEmbedded(em, store, fetch, cval);
else { else {
if (!(em instanceof ObjectIdStateManager)) if (!(em instanceof ObjectIdStateManager))
cval = embed.toEmbeddedObjectValue(cval); cval = embed.toEmbeddedObjectValue(cval);
em.store(fms[i].getIndex(), cval); if (fms[i].getHandler() != null)
cval = fms[i].getHandler().toObjectValue(fms[i], cval);
em.store(fms[i].getIndex(), cval);
} }
} }
return idx; return idx;

View File

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

View File

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

View File

@ -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<BeneContact> 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<BeneContact> getContacts() {
return contacts;
}
public void setContacts(List<BeneContact> contacts) {
this.contacts = contacts;
}
}

View File

@ -86,7 +86,8 @@ public class TestMappedById extends SingleEMFTestCase {
Person2.class, Person3.class, MedicalHistory3.class, Person2.class, Person3.class, MedicalHistory3.class,
Person4.class, PersonId4.class, MedicalHistory4.class, Person4.class, PersonId4.class, MedicalHistory4.class,
Dependent3.class, Employee3.class, DependentId3.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; return e;
} }
public void testEnumInEmbeddedId() {
EntityManager em = emf.createEntityManager();
Beneficiary b = new Beneficiary();
b.setId("b8");
List<BeneContact> contacts = new ArrayList<BeneContact>();
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());
}
} }