mirror of https://github.com/apache/openjpa.git
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:
parent
e2fef6a276
commit
9d7360cb35
|
@ -582,7 +582,8 @@ public class JDBCStoreQuery
|
||||||
* returns INVALID. Also returns INVALID if field is dependent.
|
* returns INVALID. Also returns INVALID if field is dependent.
|
||||||
*/
|
*/
|
||||||
private Table getTable(FieldMapping fm, Table table) {
|
private Table getTable(FieldMapping fm, Table table) {
|
||||||
if (fm.getCascadeDelete() != ValueMetaData.CASCADE_NONE)
|
if (fm.getCascadeDelete() != ValueMetaData.CASCADE_NONE
|
||||||
|
&& !fm.isEmbeddedPC())
|
||||||
return INVALID;
|
return INVALID;
|
||||||
|
|
||||||
Column[] columns = fm.getColumns();
|
Column[] columns = fm.getColumns();
|
||||||
|
|
|
@ -23,9 +23,10 @@ import java.util.List;
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.EntityTransaction;
|
import javax.persistence.EntityTransaction;
|
||||||
|
|
||||||
|
import org.apache.openjpa.persistence.test.SQLListenerTestCase;
|
||||||
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
||||||
|
|
||||||
public class TestEmbedded extends SingleEMFTestCase {
|
public class TestEmbedded extends SQLListenerTestCase {
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
super.setUp(BaseEntity.class, Address.class, Geocode.class,
|
super.setUp(BaseEntity.class, Address.class, Geocode.class,
|
||||||
CLEAR_TABLES);
|
CLEAR_TABLES);
|
||||||
|
@ -77,6 +78,47 @@ public class TestEmbedded extends SingleEMFTestCase {
|
||||||
assertEquals(a2.getGeocode().getLatitude(),1.0f);
|
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) {
|
private void persistAddress(Address address) {
|
||||||
final EntityManager em = emf.createEntityManager();
|
final EntityManager em = emf.createEntityManager();
|
||||||
final EntityTransaction tx = em.getTransaction();
|
final EntityTransaction tx = em.getTransaction();
|
||||||
|
|
|
@ -48,7 +48,7 @@ public class TestNonstandardMappingAnnotations
|
||||||
setUp(NonstandardMappingEntity.class, ExtensionsEntity.class,
|
setUp(NonstandardMappingEntity.class, ExtensionsEntity.class,
|
||||||
NonstandardMappingMappedSuper.class, EmbedValue2.class,
|
NonstandardMappingMappedSuper.class, EmbedValue2.class,
|
||||||
EmbedValue.class,
|
EmbedValue.class,
|
||||||
CLEAR_TABLES);
|
CLEAR_TABLES, RETAIN_DATA);
|
||||||
|
|
||||||
// trigger complete resolution of metadata etc.
|
// trigger complete resolution of metadata etc.
|
||||||
emf.createEntityManager().close();
|
emf.createEntityManager().close();
|
||||||
|
|
|
@ -61,6 +61,8 @@ public abstract class PersistenceTestCase
|
||||||
extends TestCase {
|
extends TestCase {
|
||||||
private static FixedMap _emfs = new FixedMap();
|
private static FixedMap _emfs = new FixedMap();
|
||||||
public static final String FRESH_EMF = "Creates new EntityManagerFactory";
|
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
|
* Marker object you pass to {@link #setUp} to indicate that the
|
||||||
* database table rows should be cleared.
|
* database table rows should be cleared.
|
||||||
|
@ -118,6 +120,10 @@ public abstract class PersistenceTestCase
|
||||||
fresh = true;
|
fresh = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (props[i] == RETAIN_DATA) {
|
||||||
|
retainDataOnTearDown= true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (prop) {
|
if (prop) {
|
||||||
map.put(props[i - 1], props[i]);
|
map.put(props[i - 1], props[i]);
|
||||||
prop = false;
|
prop = false;
|
||||||
|
@ -262,7 +268,8 @@ public abstract class PersistenceTestCase
|
||||||
// before issuing delete statements on a new entity manager.
|
// before issuing delete statements on a new entity manager.
|
||||||
if (closeEMs)
|
if (closeEMs)
|
||||||
closeAllOpenEMs(emf);
|
closeAllOpenEMs(emf);
|
||||||
|
if (retainDataOnTearDown)
|
||||||
|
return;
|
||||||
EntityManager em = emf.createEntityManager();
|
EntityManager em = emf.createEntityManager();
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
for (ClassMetaData meta : types) {
|
for (ClassMetaData meta : types) {
|
||||||
|
|
Loading…
Reference in New Issue