Author: hthomann Date: Mon Mar 24 16:35:08 2014 New Revision: 1580907 URL: http://svn.apache.org/r1580907 Log: OPENJPA-1986: Extra queries being generated when cascading a persist - added another/similar check to the ones added by Rick.
Modified: openjpa/branches/2.2.1.x/ (props changed) openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestCascadePersist.java Propchange: openjpa/branches/2.2.1.x/ ------------------------------------------------------------------------------ Merged /openjpa/branches/2.2.x:r1580898 Modified: openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java?rev=1580907&r1=1580906&r2=1580907&view=diff ============================================================================== --- openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java (original) +++ openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java Mon Mar 24 16:35:08 2014 @@ -267,7 +267,7 @@ class SingleFieldManager extends Transfe switch (fmd.getDeclaredTypeCode()) { case JavaTypes.PC: case JavaTypes.PC_UNTYPED: - if (!_broker.isDetachedNew() && _broker.isDetached(objval)) + if (!_broker.isDetachedNew() && _broker.isDetached(objval, _checkDbOnCascadePersist)) return; // allow but ignore _broker.persist(objval, true, call); break; Modified: openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestCascadePersist.java URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestCascadePersist.java?rev=1580907&r1=1580906&r2=1580907&view=diff ============================================================================== --- openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestCascadePersist.java (original) +++ openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cascade/TestCascadePersist.java Mon Mar 24 16:35:08 2014 @@ -21,14 +21,13 @@ package org.apache.openjpa.persistence.c import javax.persistence.EntityManager; import javax.persistence.RollbackException; -import org.apache.openjpa.persistence.test.SingleEMFTestCase; +import org.apache.openjpa.persistence.test.SQLListenerTestCase; -public class TestCascadePersist extends SingleEMFTestCase { +public class TestCascadePersist extends SQLListenerTestCase { @Override public void setUp() throws Exception { - setUp(DROP_TABLES, CascadePersistEntity.class -// , "openjpa.Log", "SQL=trace" - ); + setUp(DROP_TABLES, CascadePersistEntity.class, "openjpa.Compatibility", + "CheckDatabaseForCascadePersistToDetachedEntity=false"); } public void testCascadePersistToDetachedFailure() { @@ -70,4 +69,29 @@ public class TestCascadePersist extends // Since cpe1 is managed, it should be ignored by the cascaded persist operation. em.getTransaction().commit(); } + + /* + * Prior to OPENJPA-1986, an extra SELECT was executed in this scenario. + */ + public void testCascaseExtraneousSQL(){ + long id = System.currentTimeMillis(); + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + + CascadePersistEntity cpe = new CascadePersistEntity(id); + CascadePersistEntity cpe2 = new CascadePersistEntity(id+1); + cpe.setOther(cpe2); + + //Clear all SQL statements to make sure that during + //the persist we don't execute a select. + resetSQL(); + em.persist(cpe); + + em.getTransaction().commit(); + em.close(); + + //There should be no selects at this point, only + //inserts. + assertNotSQL("SELECT .*"); + } }