From 3c85ec758a4b6741df32d4542e64599f69adb3f3 Mon Sep 17 00:00:00 2001 From: Fay Wang Date: Fri, 30 Jan 2009 04:32:51 +0000 Subject: [PATCH] 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 --- .../openjpa/kernel/SingleFieldManager.java | 8 ++- .../persistence/relations/Customer.java | 2 +- .../relations/TestInverseEagerSQL.java | 67 ++++++++++++++++++- 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java index 7fa0bfed2..cf3d3b211 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java @@ -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(); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/Customer.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/Customer.java index e7a1f2117..220e5ae08 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/Customer.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/Customer.java @@ -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 orders = new ArrayList(); public Customer() {} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestInverseEagerSQL.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestInverseEagerSQL.java index d4be3634c..4f2892a8c 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestInverseEagerSQL.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestInverseEagerSQL.java @@ -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 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); }