OPENJPA-1900: Fix ClassCastException when serializing a proxy for an Entity that exists in an active persistence context. Patch contributed by Mark Struberg.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1050168 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Richard G. Curtis 2010-12-16 21:26:39 +00:00
parent aedc8d9dd0
commit b6024187ba
4 changed files with 157 additions and 11 deletions

View File

@ -0,0 +1,98 @@
/*
* 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.proxy;
import java.util.Date;
import java.util.UUID;
import javax.persistence.EntityManager;
import org.apache.openjpa.lib.log.Log;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import org.apache.openjpa.persistence.proxy.entities.Address;
import org.apache.openjpa.persistence.proxy.entities.Annuity;
import org.apache.openjpa.persistence.proxy.entities.AnnuityHolder;
import org.apache.openjpa.persistence.proxy.entities.AnnuityPersistebleObject;
import org.apache.openjpa.persistence.proxy.entities.Contact;
import org.apache.openjpa.persistence.proxy.entities.EquityAnnuity;
import org.apache.openjpa.persistence.proxy.entities.FixedAnnuity;
import org.apache.openjpa.persistence.proxy.entities.Payor;
import org.apache.openjpa.persistence.proxy.entities.Payout;
import org.apache.openjpa.persistence.proxy.entities.Person;
import org.apache.openjpa.persistence.proxy.entities.Rider;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Test for showing OPENJPA-1900
*/
public class TestEntitySerialize extends SingleEMFTestCase {
public void setUp() {
setUp(DROP_TABLES, Address.class, Annuity.class, AnnuityHolder.class, AnnuityPersistebleObject.class,
Contact.class, EquityAnnuity.class, FixedAnnuity.class, Payor.class, Payout.class, Person.class,
Rider.class);
}
public void testSerialization() throws Exception {
OpenJPAEntityManagerFactorySPI emf =
(OpenJPAEntityManagerFactorySPI) OpenJPAPersistence.createEntityManagerFactory("Annuity1Compat",
"org/apache/openjpa/persistence/proxy/persistence1.xml");
assertNotNull(emf);
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
Annuity ann = createAnnuity(em);
// Make sure that we can detach an Entity via serialization that is currently associated
// with a persistence context
assertNotNull(roundtrip(ann));
em.getTransaction().commit();
} finally {
closeEM(em);
}
}
private Annuity createAnnuity(EntityManager em) {
FixedAnnuity fixedAnn = new FixedAnnuity();
((FixedAnnuity) fixedAnn).setRate(10.0);
fixedAnn.setId(getId());
fixedAnn.setAmount(500.00);
fixedAnn.setAccountNumber("123456");
em.persist(fixedAnn);
EquityAnnuity equityAnn = new EquityAnnuity();
equityAnn.setId(getId());
equityAnn.setAmount(500.00);
equityAnn.setAccountNumber("123456");
equityAnn.setFundNames("Something nothing wrong");
equityAnn.setIndexRate(10.99);
equityAnn.setLastPaidAmt(100.00);
equityAnn.setPreviousAnnuity(fixedAnn);
em.persist(equityAnn);
return equityAnn;
}
private String getId() {
UUID uid = UUID.randomUUID();
return uid.toString();
}
}

View File

@ -20,6 +20,7 @@ package org.apache.openjpa.persistence.proxy.entities;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.AttributeOverride;
@ -28,6 +29,7 @@ import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
@ -36,6 +38,9 @@ import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@SuppressWarnings("serial")
@ -53,7 +58,10 @@ public class Annuity extends AnnuityPersistebleObject implements IAnnuity {
private List<IPayout> payouts = new ArrayList<IPayout>();
private List<IRider> riders = new ArrayList<IRider>();
private List<IPayor> payors = new ArrayList<IPayor>();
private List<String> comments;
private Date approvedAt;
private Annuity previousAnnuity;
public Annuity(){
}
@ -134,7 +142,27 @@ public class Annuity extends AnnuityPersistebleObject implements IAnnuity {
this.riders = riders;
}
@ElementCollection
public List<String> getComments() {
return comments;
}
public void setComments(List<String> comments) {
this.comments = comments;
}
@Temporal(TemporalType.DATE)
public Date getApprovedAt() {
return approvedAt;
}
public void setApprovedAt(Date approvedAt) {
this.approvedAt = approvedAt;
}
@OneToOne
public Annuity getPreviousAnnuity() {
return previousAnnuity;
}
public void setPreviousAnnuity(Annuity previousAnnuity) {
this.previousAnnuity = previousAnnuity;
}
}

View File

@ -19,6 +19,7 @@
package org.apache.openjpa.persistence.proxy.entities;
import java.text.DecimalFormat;
import java.text.ParseException;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
@ -48,12 +49,22 @@ public class EquityAnnuity extends Annuity implements IEquityAnnuity {
return indexRate;
}
public void setIndexRate(Double indexRate) {
this.indexRate = indexRate;
if (this.indexRate != null) {
DecimalFormat df = new DecimalFormat("#.##");
this.indexRate= new Double(df.format(indexRate));
}
}
public void setIndexRate(Double indexRate) {
if (indexRate != null) {
DecimalFormat df = new DecimalFormat("#.##");
try
{
// parse back via the DateFormat because countries might use ',' as comma separator
this.indexRate= df.parse(df.format(indexRate)).doubleValue();
}
catch (ParseException e)
{
throw new RuntimeException(e);
}
}
else {
this.indexRate = null;
}
}
}

View File

@ -18,6 +18,7 @@
*/
package org.apache.openjpa.persistence.proxy.entities;
import java.util.Date;
import java.util.List;
public interface IAnnuity extends IAnnuityObject {
@ -45,5 +46,13 @@ public interface IAnnuity extends IAnnuityObject {
public abstract List<IPayor> getPayors();
public abstract void setPayors(List<IPayor> payors);
public abstract List<String> getComments();
public abstract void setComments(List<String> comments);
public abstract Date getApprovedAt();
public void setApprovedAt(Date approvedAt);
public Annuity getPreviousAnnuity();
public void setPreviousAnnuity(Annuity previousAnnuity);
}