OPENJPA-935: Detect table for embedded PC to avoid extra select before delete.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@746640 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2009-02-22 05:52:06 +00:00
parent e2fef6a276
commit 9d7360cb35
4 changed files with 54 additions and 4 deletions

View File

@ -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();

View File

@ -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);
@ -77,6 +78,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();
final EntityTransaction tx = em.getTransaction();

View File

@ -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();

View File

@ -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) {