OPENJPA-870: orphanRemoval support with test case checked in

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@739166 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fay Wang 2009-01-30 04:32:51 +00:00
parent d81211251d
commit 3c85ec758a
3 changed files with 74 additions and 3 deletions

View File

@ -479,7 +479,13 @@ class SingleFieldManager
// manage inverses
InverseManager manager = _broker.getInverseManager();
if (manager != null)
FieldMetaData[] inverses = fmd.getInverseMetaDatas();
// if there is bi-di relation and InverseManager is on,
// use InverseManager to remove the orphans.
// for uni-directional relation or InverseManager is not on,
// we need to use OrphanRemovalManager to remove the orphans
if (manager != null && inverses.length > 0)
manager.correctRelations(_sm, fmd, objval);
else {
FieldMetaData[] orphans = fmd.getOrphanRemovalMetaDatas();

View File

@ -72,7 +72,7 @@ public class Customer {
@Version
long version;
@OneToMany(fetch=FetchType.LAZY, mappedBy="customer")
@OneToMany(fetch=FetchType.LAZY, mappedBy="customer", orphanRemoval=true)
private Collection<Order> orders = new ArrayList<Order>();
public Customer() {}

View File

@ -37,6 +37,9 @@ import org.apache.openjpa.persistence.test.SQLListenerTestCase;
public class TestInverseEagerSQL
extends SQLListenerTestCase {
public int numCustomers = 1;
public int numOrdersPerCustomer = 4;
public void setUp() {
setUp(Customer.class, Customer.CustomerKey.class, Order.class,
EntityAInverseEager.class, EntityA1InverseEager.class, EntityA2InverseEager.class,
@ -52,7 +55,7 @@ public class TestInverseEagerSQL
c.setName("customer1");
em.persist(c);
for (int i = 0; i < 4; i++) {
for (int i = 0; i < numOrdersPerCustomer; i++) {
Order order = new Order();
order.setCustomer(c);
em.persist(order);
@ -251,6 +254,68 @@ public class TestInverseEagerSQL
em.close();
}
public void testTargetOrphanRemoval() {
OpenJPAEntityManager em = emf.createEntityManager();
int count = count(Order.class);
assertEquals(numOrdersPerCustomer * numCustomers, count);
Customer.CustomerKey ck = new Customer.CustomerKey("USA", 1);
Customer c = em.find(Customer.class, ck);
Collection<Order> orders = c.getOrders();
assertEquals(numOrdersPerCustomer, orders.size());
// OrphanRemoval: remove target: the order will be deleted from db
for (Order order : orders) {
orders.remove(order);
break;
}
em.getTransaction().begin();
em.persist(c);
em.flush();
em.getTransaction().commit();
em.clear();
c = em.find(Customer.class, ck);
orders = c.getOrders();
assertEquals(numOrdersPerCustomer - 1, orders.size());
count = count(Order.class);
assertEquals(numOrdersPerCustomer * numCustomers - 1, count);
em.clear();
// OrphanRemoval: remove target: setOrders to null
c = em.find(Customer.class, ck);
c.setOrders(null);
em.getTransaction().begin();
em.persist(c);
em.flush();
em.getTransaction().commit();
em.clear();
count = count(Order.class);
assertEquals(numOrdersPerCustomer * (numCustomers - 1), count);
c = em.find(Customer.class, ck);
orders = c.getOrders();
if (orders != null)
assertEquals(0, orders.size());
em.close();
}
public void testSourceOrphanRemoval() {
OpenJPAEntityManager em = emf.createEntityManager();
// OrphanRemoval: remove source
Customer.CustomerKey ck = new Customer.CustomerKey("USA", 1);
Customer c = em.find(Customer.class, ck);
em.getTransaction().begin();
em.remove(c);
em.flush();
em.getTransaction().commit();
em.clear();
int count = count(Order.class);
assertEquals(numOrdersPerCustomer * (numCustomers - 1), count);
em.close();
}
public static void main(String[] args) {
TestRunner.run(TestInverseEagerSQL.class);
}