From b7bd2c873dd8bf84fb94ced1528e4c09ebc46249 Mon Sep 17 00:00:00 2001 From: "Richard G. Curtis" Date: Wed, 11 Apr 2012 13:40:53 +0000 Subject: [PATCH] OPENJPA-2110: Create correct proxy type when type is defined in an abstract class. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1324759 13f79535-47bb-0310-9956-ffa450edef68 --- .../openjpa/meta/ProxySetupStateManager.java | 7 ++- .../persistence/proxy/AbstractEntity.java | 56 +++++++++++++++++++ .../persistence/proxy/ConcreteEntity.java | 28 ++++++++++ .../proxy/TestProxyCollection.java | 22 +++++++- 4 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/AbstractEntity.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/ConcreteEntity.java diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ProxySetupStateManager.java b/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ProxySetupStateManager.java index d598ea815..b4455954d 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ProxySetupStateManager.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ProxySetupStateManager.java @@ -19,6 +19,7 @@ package org.apache.openjpa.meta; import java.io.ObjectOutput; +import java.lang.reflect.Modifier; import java.util.Calendar; import java.util.SortedMap; import java.util.SortedSet; @@ -46,9 +47,9 @@ class ProxySetupStateManager public void setProxyData(PersistenceCapable pc, ClassMetaData meta) { FieldMetaData[] fmds = meta.getFields(); for (int i = 0; i < fmds.length; i++) { - if (fmds[i].getDefiningMetaData() != meta) - continue; - + // This method only gets called for concrete types. We need to do this processing for fields that might + // not be owned by pc. + switch (fmds[i].getDeclaredTypeCode()) { case JavaTypes.CALENDAR: pc.pcProvideField(fmds[i].getIndex()); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/AbstractEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/AbstractEntity.java new file mode 100644 index 000000000..ce27e8fe1 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/AbstractEntity.java @@ -0,0 +1,56 @@ +/* + * 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.LinkedHashSet; +import java.util.Set; + +import javax.persistence.CascadeType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.MappedSuperclass; +import javax.persistence.OneToMany; +import javax.persistence.Version; + +@MappedSuperclass +public abstract class AbstractEntity { + @Id + @GeneratedValue + private int id; + + @Version + int version; + + @OneToMany(cascade = CascadeType.ALL) + private Set items = new LinkedHashSet(); + + public int getId() { + return id; + } + + public Set getItems() { + return items; + } + + public void addItem(ConcreteEntity ce) { + if (items == null) + items = new LinkedHashSet(); + items.add(ce); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/ConcreteEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/ConcreteEntity.java new file mode 100644 index 000000000..907124c89 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/ConcreteEntity.java @@ -0,0 +1,28 @@ +/* + * 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 javax.persistence.Basic; +import javax.persistence.Entity; + +@Entity +public class ConcreteEntity extends AbstractEntity { + @Basic + int field; +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/TestProxyCollection.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/TestProxyCollection.java index ff5c8d3a8..0bd04625f 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/TestProxyCollection.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/proxy/TestProxyCollection.java @@ -18,6 +18,8 @@ */ package org.apache.openjpa.persistence.proxy; +import java.util.LinkedHashSet; + import javax.persistence.EntityManager; import org.apache.openjpa.persistence.test.SingleEMFTestCase; @@ -36,7 +38,7 @@ import org.apache.openjpa.util.ProxyCollection; */ public class TestProxyCollection extends SingleEMFTestCase { public void setUp() { - super.setUp(CLEAR_TABLES, TreeNode.class); + super.setUp(CLEAR_TABLES, TreeNode.class, ConcreteEntity.class, AbstractEntity.class); } /** * Tests that a uniform tree is created with expected fan outs at each @@ -103,6 +105,24 @@ public class TestProxyCollection extends SingleEMFTestCase { int[] modifier = {1,2,3}; // remove 1 from each Level createModifyAndMerge(original, modifier); } + + public void testCreateCorrectType() { + ConcreteEntity ce = new ConcreteEntity(); + ce.addItem(ce); + + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + + em.persist(ce); + em.getTransaction().commit(); + em.clear(); + + ce = em.find(ConcreteEntity.class, ce.getId()); + assertNotNull(ce); + Class proxyCls = ce.getItems().getClass(); + assertTrue(proxyCls + " is not assignableFrom " + LinkedHashSet.class, + LinkedHashSet.class.isAssignableFrom(proxyCls)); + } /** * Create a uniform tree with original fanout. * Persist.