From b56afb256c2ca9eb3ee3ecfd75fec5ef2f88e9c9 Mon Sep 17 00:00:00 2001 From: "Richard G. Curtis" Date: Fri, 17 Dec 2010 12:19:46 +0000 Subject: [PATCH] OPENJPA-1900: Merge changes from trunk. Changes contributed by Mark Struberg. git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/2.1.x@1050361 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/openjpa/util/Proxies.java | 14 ++- .../proxy/TestEntitySerialize.java | 98 +++++++++++++++++++ .../persistence/proxy/entities/Annuity.java | 34 ++++++- .../proxy/entities/EquityAnnuity.java | 25 +++-- .../persistence/proxy/entities/IAnnuity.java | 11 ++- 5 files changed, 166 insertions(+), 16 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/TestEntitySerialize.java diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/util/Proxies.java b/openjpa-kernel/src/main/java/org/apache/openjpa/util/Proxies.java index ae273779f..bcbe973fa 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/util/Proxies.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/util/Proxies.java @@ -127,8 +127,15 @@ public class Proxies { ClassMetaData meta = null; // if null, no proxies? boolean useDSFForUnproxy = false; // default to false for old 1.0 behavior - // DetachedStateMnager has no context or metadata, so we can't get configuration settings - if (!proxy.getOwner().isDetached()) { + // Don't rely on sm.isDetached() method because if we are serializing an attached Entity + // the sm will still be a StateManagerImpl, but isDetached() will return true. + + // Using a DetachedStateManager, so use the new flag since there is no context or + // metadata + if (sm instanceof DetachedStateManager) { + useDSFForUnproxy = ((DetachedStateManager) sm).getUseDSFForUnproxy(); + } else{ + // DetachedStateManager has no context or metadata, so we can't get configuration settings Compatibility compat = null; meta = sm.getMetaData(); if (meta != null) { @@ -142,9 +149,6 @@ public class Proxies { // new 2.0 behavior of using DetachedStateField to determine unproxy during serialization useDSFForUnproxy = !compat.getIgnoreDetachedStateFieldForProxySerialization(); } - } else { - // Using a DetachedStateManager, so use the new flag since there is no context or metadata - useDSFForUnproxy = ((DetachedStateManager)sm).getUseDSFForUnproxy(); } if (useDSFForUnproxy) { diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/TestEntitySerialize.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/TestEntitySerialize.java new file mode 100644 index 000000000..8b59cf58c --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/TestEntitySerialize.java @@ -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(); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/Annuity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/Annuity.java index 96816fdbc..62a508f98 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/Annuity.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/Annuity.java @@ -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 payouts = new ArrayList(); private List riders = new ArrayList(); private List payors = new ArrayList(); + private List 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 getComments() { + return comments; + } + public void setComments(List 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; + } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/EquityAnnuity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/EquityAnnuity.java index 453100aa2..e54e3eb38 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/EquityAnnuity.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/EquityAnnuity.java @@ -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; + } + } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/IAnnuity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/IAnnuity.java index 36d7d935e..d8aec6b95 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/IAnnuity.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/entities/IAnnuity.java @@ -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 getPayors(); public abstract void setPayors(List payors); - + + public abstract List getComments(); + public abstract void setComments(List comments); + + public abstract Date getApprovedAt(); + public void setApprovedAt(Date approvedAt); + + public Annuity getPreviousAnnuity(); + public void setPreviousAnnuity(Annuity previousAnnuity); }