Author: tfischer
Date: Wed Sep  3 19:36:14 2014
New Revision: 1622332

URL: http://svn.apache.org/r1622332
Log:
TORQUE-318 retrieveByPK now throws NoRowsException instead of TorqueException 
when it can't find a primary key.

Modified:
    
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPK.vm
    
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/RetrieveByPkTest.java

Modified: 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPK.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/retrieveByPK.vm?rev=1622332&r1=1622331&r2=1622332&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPK.vm
 (original)
+++ 
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPK.vm
 Wed Sep  3 19:36:14 2014
@@ -135,13 +135,17 @@
         criteria.and(${peerClassName}.${peerColumnName}, $columnField);
     #end
         List<${dbObjectClassName}> v = doSelect(criteria, con);
-        if (v.size() == 1)
+        if (v.size() == 0)
         {
-            return v.get(0);
+            throw new NoRowsException("Failed to select a row.");
+        }
+        else if (v.size() > 1)
+        {
+            throw new TooManyRowsException("Failed to select only one row.");
         }
         else
         {
-            throw new TorqueException("Failed to select one and only one 
row.");
+            return ($dbObjectClassName)v.get(0);
         }
     }
   #end

Modified: 
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/RetrieveByPkTest.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/RetrieveByPkTest.java?rev=1622332&r1=1622331&r2=1622332&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/RetrieveByPkTest.java
 (original)
+++ 
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/RetrieveByPkTest.java
 Wed Sep  3 19:36:14 2014
@@ -20,6 +20,7 @@ package org.apache.torque.generated.peer
  */
 
 import java.math.BigDecimal;
+import java.util.Date;
 import java.util.List;
 
 import org.apache.torque.BaseDatabaseTestCase;
@@ -28,6 +29,7 @@ import org.apache.torque.om.NumberKey;
 import org.apache.torque.om.ObjectKey;
 import org.apache.torque.test.dbobject.Author;
 import org.apache.torque.test.peer.AuthorPeer;
+import org.apache.torque.test.peer.MultiPkPeer;
 
 /**
  * Tests the retrieveByPk methods.
@@ -82,6 +84,24 @@ public class RetrieveByPkTest extends Ba
     }
 
     /**
+     * Tests retrieveByPk using a non-existent primary key
+     * for an object with multiple PKs (TORQUE-318)
+     *
+     * @throws Exception if the test fails
+     */
+    public void testRetrieveByNonExistingPkMultiplePKs() throws Exception
+    {
+        try
+        {
+            MultiPkPeer.retrieveByPK("", 1, "", 1, (byte) 1, (short) 1, 1l, 
1d, 1d, new Date(1l));
+        }
+        catch (NoRowsException e)
+        {
+            assertEquals("Failed to select a row.", e.getMessage());
+        }
+    }
+
+    /**
      * Tests retrieveByPk using a key object with a value of null.
      *
      * @throws Exception if the test fails



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscr...@db.apache.org
For additional commands, e-mail: torque-dev-h...@db.apache.org

Reply via email to