Author: tfischer Date: Sat Jul 6 02:44:58 2013 New Revision: 1500192 URL: http://svn.apache.org/r1500192 Log: TORQUE-291 SetAndSave methods do not work for empty lists
Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/setAndSaveReferencing.vm db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/SetAndSaveTest.java Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/setAndSaveReferencing.vm URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/setAndSaveReferencing.vm?rev=1500192&r1=1500191&r2=1500192&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/setAndSaveReferencing.vm (original) +++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/setAndSaveReferencing.vm Sat Jul 6 02:44:58 2013 @@ -182,41 +182,53 @@ toSaveKeys.add(toSaveKey); } } + List<${foreignDbObjectClassName}> intersection; + if (toSaveKeys.isEmpty()) + { + intersection = new ArrayList<${foreignDbObjectClassName}>(); + } + else + { #if ($foreignPrimaryKeyColumnElements.size() == 1) #set ( $columnElement = $foreignPrimaryKeyColumnElements.get(0) ) #set ( $peerColumnName = $columnElement.getAttribute("peerColumnName")) - criteria.and(${foreignTablePeerClassName}.${peerColumnName}, toSaveKeys, Criteria.IN); + criteria.and( + ${foreignTablePeerClassName}.${peerColumnName}, + toSaveKeys, + Criteria.IN); #else - Criterion toSaveKeyCriterion = null; - for (ObjectKey toSaveKey : toSaveKeys) - { - SimpleKey[] toSavePrimaryKeys = (SimpleKey[]) toSaveKey.getValue(); + Criterion toSaveKeyCriterion = null; + for (ObjectKey toSaveKey : toSaveKeys) + { + SimpleKey[] toSavePrimaryKeys = (SimpleKey[]) toSaveKey.getValue(); #set ( $i = 0 ) #foreach ($columnElement in $foreignPrimaryKeyColumnElements) #set ( $columnElement = $referenceElement.getChild("local-column").getChild("column") ) #set ( $peerColumnName = $columnElement.getAttribute("peerColumnName")) - Criterion p$i = new Criterion( - ${foreignTablePeerClassName}.${peerColumnName}, - toSavePrimaryKeys[$i]); + Criterion p$i = new Criterion( + ${foreignTablePeerClassName}.${peerColumnName}, + toSavePrimaryKeys[$i]); #set ( $j = $i - 1 ) #if ($i > 0) - p${j}.and(p${i}); + p${j}.and(p${i}); #end #set ( $i = $i + 1 ) #end - if (toSaveKeyCriterion == null) - { - toSaveKeyCriterion = p0; - } - else - { - toSaveKeyCriterion.or(p0); + if (toSaveKeyCriterion == null) + { + toSaveKeyCriterion = p0; + } + else + { + toSaveKeyCriterion.or(p0); + } } - } - criteria.and(toSaveKeyCriterion); + criteria.and(toSaveKeyCriterion); #end - List<${foreignDbObjectClassName}> intersection - = ${foreignTablePeerClassName}.doSelect(criteria, connection); + intersection = ${foreignTablePeerClassName}.doSelect( + criteria, + connection); + } $fieldType linkedCache; if (toLinkTo.${isInitialized}()) { linkedCache = toLinkTo.${getter}(); @@ -263,40 +275,46 @@ // delete elements not in intersection Criteria deleteCriteria = new Criteria(); deleteCriteria.where(onlyReferencingCriterion); + if (!toSaveKeys.isEmpty()) + { #if ($foreignPrimaryKeyColumnElements.size() == 1) #set ( $columnElement = $foreignPrimaryKeyColumnElements.get(0) ) #set ( $peerColumnName = $columnElement.getAttribute("peerColumnName")) - deleteCriteria.and(${foreignTablePeerClassName}.${peerColumnName}, toSaveKeys, Criteria.NOT_IN); + deleteCriteria.and( + ${foreignTablePeerClassName}.${peerColumnName}, + toSaveKeys, + Criteria.NOT_IN); #else - toSaveKeyCriterion = null; - for (ObjectKey toSaveKey : toSaveKeys) - { - SimpleKey[] toSavePrimaryKeys = (SimpleKey[]) toSaveKey.getValue(); + Criterion toSaveKeyCriterion = null; + for (ObjectKey toSaveKey : toSaveKeys) + { + SimpleKey[] toSavePrimaryKeys = (SimpleKey[]) toSaveKey.getValue(); #set ( $i = 0 ) #foreach ($columnElement in $foreignPrimaryKeyColumnElements) #set ( $columnElement = $referenceElement.getChild("local-column").getChild("column") ) #set ( $peerColumnName = $columnElement.getAttribute("peerColumnName")) - Criterion p$i = new Criterion( - ${foreignTablePeerClassName}.${peerColumnName}, - toSavePrimaryKeys[$i], - Criteria.NOT_EQUAL); + Criterion p$i = new Criterion( + ${foreignTablePeerClassName}.${peerColumnName}, + toSavePrimaryKeys[$i], + Criteria.NOT_EQUAL); #set ( $j = $i - 1 ) #if ($i > 0) - p${j}.or(p${i}); + p${j}.or(p${i}); #end #set ( $i = $i + 1 ) #end - if (toSaveKeyCriterion == null) - { - toSaveKeyCriterion = p0; - } - else - { - toSaveKeyCriterion.and(p0); + if (toSaveKeyCriterion == null) + { + toSaveKeyCriterion = p0; + } + else + { + toSaveKeyCriterion.and(p0); + } } - } - deleteCriteria.and(toSaveKeyCriterion); + deleteCriteria.and(toSaveKeyCriterion); #end + } ${foreignTablePeerClassName}.doDelete(deleteCriteria, connection); } #end Modified: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/SetAndSaveTest.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/SetAndSaveTest.java?rev=1500192&r1=1500191&r2=1500192&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/SetAndSaveTest.java (original) +++ db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/SetAndSaveTest.java Sat Jul 6 02:44:58 2013 @@ -505,4 +505,76 @@ public class SetAndSaveTest extends Base = OIntegerPkPeer.retrieveByPK(oIntegerPk.getId()); assertEquals("oIntegerPk3", oIntegerPkFromDb.getName()); } + + /** + * Tests whether the setAndSave method works for empty lists + * where a non-composite foreign key links the two objects. + * + * @throws Exception if the test fails + */ + public void testSetAndSaveEmptyListSingleFk() throws Exception + { + ForeignKeySchemaData.clearTablesInDatabase(); + ForeignKeySchemaData testData + = ForeignKeySchemaData.getDefaultTestData(); + testData.save(); + + OIntegerPk oIntegerPk = testData.getOIntegerPkList().get(2); + List<NullableOIntegerFk> fkList = new ArrayList<NullableOIntegerFk>(); + + OIntegerPkPeer.setAndSaveNullableOIntegerFks( + oIntegerPk, + fkList); + + List<NullableOIntegerFk> cachedFks + = oIntegerPk.getNullableOIntegerFks(); + assertEquals(0, cachedFks.size()); + + // check database + List<NullableOIntegerFk> dbFks + = testData.getNullableOIntegerFkList(); + // remove entries where oIntegerPk is referenced + dbFks.remove(2); + dbFks.remove(1); + ForeignKeySchemaData.assertNullableOIntegerFksInDatabaseEquals( + dbFks); + } + + /** + * Tests whether the setAndSave method works for empty lists + * where a composite foreign key links the two objects. + * + * @throws Exception if the test fails + */ + public void testSetAndSaveEmptyListCompositeFk() throws Exception + { + ForeignKeySchemaData.clearTablesInDatabase(); + ForeignKeySchemaData testData + = ForeignKeySchemaData.getDefaultTestData(); + testData.save(); + + CompIntegerVarcharPk compIntegerVarcharPk + = testData.getCompositeIntegerVarcharPkList().get(2); + List<CompIntegerVarcharFk> fkList + = new ArrayList<CompIntegerVarcharFk>(); + + CompIntegerVarcharPkPeer.setAndSaveCompIntegerVarcharFks( + compIntegerVarcharPk, + fkList); + + List<CompIntegerVarcharFk> cachedFks + = compIntegerVarcharPk.getCompIntegerVarcharFks(); + assertEquals(0, cachedFks.size()); + + // check database + List<CompIntegerVarcharFk> dbFks + = testData.getCompositeIntegerVarcharFkList(); + // remove entries where oIntegerPk is referenced + dbFks.remove(2); + dbFks.remove(1); + ForeignKeySchemaData.assertCompositeIntegerVarcharFksInDatabaseEquals( + dbFks); + } + + } --------------------------------------------------------------------- To unsubscribe, e-mail: torque-dev-unsubscr...@db.apache.org For additional commands, e-mail: torque-dev-h...@db.apache.org