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
This commit is contained in:
Richard G. Curtis 2012-04-11 13:40:53 +00:00
parent 3bfaadf1c1
commit b7bd2c873d
4 changed files with 109 additions and 4 deletions

View File

@ -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,8 +47,8 @@ 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:

View File

@ -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<ConcreteEntity> items = new LinkedHashSet<ConcreteEntity>();
public int getId() {
return id;
}
public Set<ConcreteEntity> getItems() {
return items;
}
public void addItem(ConcreteEntity ce) {
if (items == null)
items = new LinkedHashSet<ConcreteEntity>();
items.add(ce);
}
}

View File

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

View File

@ -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.