diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java index f9bd67402..868382b83 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java @@ -582,7 +582,8 @@ public class JDBCStoreQuery * returns INVALID. Also returns INVALID if field is dependent. */ private Table getTable(FieldMapping fm, Table table) { - if (fm.getCascadeDelete() != ValueMetaData.CASCADE_NONE) + if (fm.getCascadeDelete() != ValueMetaData.CASCADE_NONE + && !fm.isEmbeddedPC()) return INVALID; Column[] columns = fm.getColumns(); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbedded.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbedded.java index af14a5245..ed5e08dba 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbedded.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbedded.java @@ -23,9 +23,10 @@ import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityTransaction; +import org.apache.openjpa.persistence.test.SQLListenerTestCase; import org.apache.openjpa.persistence.test.SingleEMFTestCase; -public class TestEmbedded extends SingleEMFTestCase { +public class TestEmbedded extends SQLListenerTestCase { public void setUp() { super.setUp(BaseEntity.class, Address.class, Geocode.class, CLEAR_TABLES); @@ -76,6 +77,47 @@ public class TestEmbedded extends SingleEMFTestCase { assertEquals(a2.getGeocode().getLatitude(),1.0f); } + + public void testDeleteEmbeddedDoesNotSelectBeforeDelete() { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + String[] streets = {"S1", "S2", "S3"}; + String[] cities = {"C1", "C2", "C3"}; + String[] states = {"AB", "CD", "EF"}; + int[] zips = {123456, 345678, 456789}; + + for (int i = 0; i < streets.length; i++) { + Address a = new Address(); + a.setStreetAddress(streets[i]); + a.setCity(cities[i]); + a.setState(states[i]); + a.setZip(zips[i]); + Geocode g = new Geocode(); + g.setLatitude(i+1.0f); + g.setLongtitude(i+6.0f); + a.setGeocode(g); + em.persist(a); + } + em.getTransaction().commit(); + + em = emf.createEntityManager(); + em.getTransaction().begin(); + sql.clear(); + int count = em.createQuery("DELETE FROM Address a WHERE a.zip=:zip") + .setParameter("zip", zips[0]) + .executeUpdate(); + assertEquals(1, count); + em.getTransaction().commit(); + assertEquals(1, sql.size()); + + em.getTransaction().begin(); + sql.clear(); + count = em.createQuery("DELETE FROM Address").executeUpdate(); + assertEquals(streets.length-1, count); + assertTrue(count>1); + em.getTransaction().commit(); + assertEquals(1, sql.size()); + } private void persistAddress(Address address) { final EntityManager em = emf.createEntityManager(); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestNonstandardMappingAnnotations.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestNonstandardMappingAnnotations.java index 34f7d762a..96351e916 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestNonstandardMappingAnnotations.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestNonstandardMappingAnnotations.java @@ -48,7 +48,7 @@ public class TestNonstandardMappingAnnotations setUp(NonstandardMappingEntity.class, ExtensionsEntity.class, NonstandardMappingMappedSuper.class, EmbedValue2.class, EmbedValue.class, - CLEAR_TABLES); + CLEAR_TABLES, RETAIN_DATA); // trigger complete resolution of metadata etc. emf.createEntityManager().close(); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java index 120cbcd24..4dcfa6bd1 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java @@ -61,6 +61,8 @@ public abstract class PersistenceTestCase extends TestCase { private static FixedMap _emfs = new FixedMap(); public static final String FRESH_EMF = "Creates new EntityManagerFactory"; + public static final String RETAIN_DATA = "Retain data after test run"; + private boolean retainDataOnTearDown; /** * Marker object you pass to {@link #setUp} to indicate that the * database table rows should be cleared. @@ -118,6 +120,10 @@ public abstract class PersistenceTestCase fresh = true; continue; } + if (props[i] == RETAIN_DATA) { + retainDataOnTearDown= true; + continue; + } if (prop) { map.put(props[i - 1], props[i]); prop = false; @@ -262,7 +268,8 @@ public abstract class PersistenceTestCase // before issuing delete statements on a new entity manager. if (closeEMs) closeAllOpenEMs(emf); - + if (retainDataOnTearDown) + return; EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); for (ClassMetaData meta : types) {