Author: curtisr7
Date: Thu Dec 16 21:26:39 2010
New Revision: 1050168
URL: http://svn.apache.org/viewvc?rev=1050168&view=rev
Log:
OPENJPA-1900: Fix ClassCastException when serializing a proxy for an Entity
that exists in an active persistence context. Patch contributed by Mark
Struberg.
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/TestEntitySerialize.java
(with props)
Modified:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/Annuity.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/EquityAnnuity.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/IAnnuity.java
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/TestEntitySerialize.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/TestEntitySerialize.java?rev=1050168&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/TestEntitySerialize.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/TestEntitySerialize.java
Thu Dec 16 21:26:39 2010
@@ -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();
+ }
+}
Propchange:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/TestEntitySerialize.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/Annuity.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/Annuity.java?rev=1050168&r1=1050167&r2=1050168&view=diff
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/Annuity.java
(original)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/Annuity.java
Thu Dec 16 21:26:39 2010
@@ -20,6 +20,7 @@ package org.apache.openjpa.persistence.p
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 AnnuityPers
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 AnnuityPers
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;
+ }
}
Modified:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/EquityAnnuity.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/EquityAnnuity.java?rev=1050168&r1=1050167&r2=1050168&view=diff
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/EquityAnnuity.java
(original)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/EquityAnnuity.java
Thu Dec 16 21:26:39 2010
@@ -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 Annui
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;
+ }
+ }
}
Modified:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/IAnnuity.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/IAnnuity.java?rev=1050168&r1=1050167&r2=1050168&view=diff
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/IAnnuity.java
(original)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/IAnnuity.java
Thu Dec 16 21:26:39 2010
@@ -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 IAnnui
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);
}