OPENJPA-2068: Fixing a bug where we were incorrectly removing LRS proxy fields while loading the proxy.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1233434 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Richard G. Curtis 2012-01-19 15:33:01 +00:00
parent 6bed19c848
commit 2c65619be4
4 changed files with 223 additions and 3 deletions

View File

@ -40,6 +40,7 @@ import org.apache.openjpa.util.ChangeTracker;
import org.apache.openjpa.util.Exceptions;
import org.apache.openjpa.util.ImplHelper;
import org.apache.openjpa.util.InvalidStateException;
import org.apache.openjpa.util.LRSProxy;
import org.apache.openjpa.util.MapChangeTracker;
import org.apache.openjpa.util.ObjectId;
import org.apache.openjpa.util.Proxies;
@ -154,7 +155,9 @@ class SingleFieldManager extends TransferFieldManager implements Serializable {
}
/**
* If the current field is a usable proxy, return it; else return null.
* If the current field is a usable proxy and it should be a proxy, return it; else return null.
*
* This method will skim out Calendar instances that were proxied before we knew if they need to be proxied.
*/
private Proxy checkProxy(FieldMetaData fmd) {
if (!(objval instanceof Proxy))
@ -162,7 +165,8 @@ class SingleFieldManager extends TransferFieldManager implements Serializable {
Proxy proxy = (Proxy) objval;
if (proxy.getOwner() == null || Proxies.isOwner(proxy, _sm, field)) {
if(fmd.getProxyType().isAssignableFrom(proxy.getClass())){
if (fmd.getProxyType().isAssignableFrom(proxy.getClass())
|| (fmd.isLRS() && (objval instanceof LRSProxy))) {
return proxy;
}
}

View File

@ -19,11 +19,15 @@
package org.apache.openjpa.persistence.relations;
import java.util.Iterator;
import javax.persistence.EntityManager;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import org.apache.openjpa.persistence.relations.entity.LrsEntityA;
import org.apache.openjpa.persistence.relations.entity.LrsEntityB;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
@ -37,7 +41,7 @@ public class TestLRS
private long id;
public void setUp() {
setUp(LRSEntity.class, BasicEntity.class, CLEAR_TABLES,
setUp(LrsEntityA.class, LrsEntityB.class, LRSEntity.class, BasicEntity.class, CLEAR_TABLES,
"openjpa.Compatibility", "default(copyOnDetach=true," +
"cascadeWithDetach=true)");
@ -88,6 +92,20 @@ public class TestLRS
assertMerge(lrs);
}
public void testRelationships(){
OpenJPAEntityManager em = emf.createEntityManager();
LrsEntityA a = new LrsEntityA("name");
LrsEntityB b = new LrsEntityB("name-b", a);
em.getTransaction().begin();
em.persist(a);
em.persist(b);
em.getTransaction().commit();
em.clear();
LrsEntityA a1 = em.find(LrsEntityA.class, a.getId());
assertEquals(1, a1.getEntitybs().size());
}
private void assertLRS(LRSEntity lrs, String name) {
assertNotNull(lrs);
assertEquals(name, lrs.getName());
@ -113,6 +131,7 @@ public class TestLRS
em.close();
}
public static void main(String[] args) {
TestRunner.run(TestLRS.class);
}

View File

@ -0,0 +1,105 @@
/*
* 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.relations.entity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.annotation.Generated;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.apache.openjpa.persistence.LRS;
import org.apache.openjpa.persistence.jdbc.Index;
@Entity
@Table(name = "LrsEntityB")
public class LrsEntityA implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Index(name = "lrsa_index", unique = true)
@GeneratedValue(strategy=GenerationType.AUTO)
Integer id;
@Column(length = 30)
String name;
@Basic(fetch = FetchType.LAZY)
int age;
@OneToMany(mappedBy = "entitya", cascade = CascadeType.ALL)
// if fk creates table need cascadeRemove on inverse
@LRS
public Collection<LrsEntityB> entitybs;
public LrsEntityA() {
this.name = "none";
this.entitybs = new ArrayList<LrsEntityB>();
}
public LrsEntityA(String nam) {
this.name = nam;
entitybs = new ArrayList<LrsEntityB>();
}
public LrsEntityA(int id, String nam, int age) {
this.id = id;
this.name = nam;
this.age = age;
entitybs = new ArrayList<LrsEntityB>();
}
public Collection<LrsEntityB> getEntitybs() {
return entitybs;
}
public void setEntitybs(Collection<LrsEntityB> entitybs) {
this.entitybs = entitybs;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,92 @@
/*
* 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.relations.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.openjpa.persistence.jdbc.ForeignKey;
import org.apache.openjpa.persistence.jdbc.ForeignKeyAction;
import org.apache.openjpa.persistence.jdbc.Index;
@Entity
@Table(name = "LrsEntityB")
public class LrsEntityB implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Index(name = "lrsb_index", unique = true)
@GeneratedValue(strategy=GenerationType.AUTO)
Integer id;
@Column(length = 30)
String name;
@ManyToOne()
@ForeignKey(deleteAction = ForeignKeyAction.NULL)
// needed for native dropandcreatetest
LrsEntityA entitya;
public LrsEntityB() {
this.name = "none";
this.entitya = null;
}
public LrsEntityB(String nam) {
this.name = nam;
this.entitya = null;
}
public LrsEntityB(String nam, LrsEntityA entitya) {
this.name = nam;
this.entitya = entitya;
if (entitya != null)
entitya.getEntitybs().add(this);
}
public LrsEntityA getEntitya() {
return entitya;
}
public void setEntitya(LrsEntityA entitya) {
this.entitya = entitya;
if (entitya != null)
entitya.getEntitybs().add(this);
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}