Author: tfischer Date: Tue Oct 6 02:44:30 2015 New Revision: 1706946 URL: http://svn.apache.org/viewvc?rev=1706946&view=rev Log: TORQUE-334 - Generate new methods retrieveByTypedPKs and retrieveByObjectKeys. - Made generated methods retrieveByPKs deprecated. - Inserted generation option torque.om.retrieve.bypk.deprecated = true for disabling the generation of the deprecated methods - Added tests in the test project
Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/conf/options.properties db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/retrieveByPKs.vm db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencedObject.vm db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPKs.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/conf/options.properties URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/conf/options.properties?rev=1706946&r1=1706945&r2=1706946&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/conf/options.properties (original) +++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/conf/options.properties Tue Oct 6 02:44:30 2015 @@ -294,3 +294,8 @@ torque.om.enumNamePrefix = # The suffix for the name of enum types. torque.om.enumNameSuffix = Enum +# Whether to generate the deprecated retrieveByPKs methods, +# which have been renamed to retrieveByObjectKeys. +# These methods only provide backwards compatibility, +# the same functionality is provided by the retrieveByObjectKeys methods. +torque.om.retrieve.bypk.deprecated = true \ No newline at end of file Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/retrieveByPKs.vm URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/retrieveByPKs.vm?rev=1706946&r1=1706945&r2=1706946&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/retrieveByPKs.vm (original) +++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/retrieveByPKs.vm Tue Oct 6 02:44:30 2015 @@ -29,13 +29,92 @@ ## #set ( $primaryKeyColumnElements = $torqueGen.getChild("primary-keys").getChildren("column")) #if (!$primaryKeyColumnElements.isEmpty()) + #if ($primaryKeyColumnElements.size() == 1) + #set ( $columnElement = $primaryKeyColumnElements.get(0) ) + #set ( $columnFieldType = $columnElement.getAttribute("fieldObjectType")) /** - * Retrieve a multiple objects by pk + * Retrieve multiple objects by pk. * * @param pks List of primary keys + * + * @return the list of matching objects, not null. + * + * @throws TorqueException Any exceptions caught during processing will be + * rethrown wrapped into a TorqueException. + */ + public static List<${dbObjectClassName}> retrieveByTypedPKs(Collection<${columnFieldType}> pks) + throws TorqueException + { + return ${peerImplGetter}().retrieveByTypedPKs(pks); + } + + /** + * Retrieve multiple objects by pk. + * + * @param pks List of primary keys + * @param dbcon the connection to use + * + * @return the list of matching objects, not null. + * + * @throws TorqueException Any exceptions caught during processing will be + * rethrown wrapped into a TorqueException. + */ + public static List<${dbObjectClassName}> retrieveByTypedPKs( + Collection<${columnFieldType}> pks, + Connection dbcon) + throws TorqueException + { + return ${peerImplGetter}().retrieveByTypedPKs(pks, dbcon); + } + + #end + /** + * Retrieve a multiple objects by pk. + * + * @param pks List of primary keys + * + * @return the list of matching objects, not null. + * + * @throws TorqueException Any exceptions caught during processing will be + * rethrown wrapped into a TorqueException. + */ + public static List<${dbObjectClassName}> retrieveByObjectKeys(Collection<ObjectKey> pks) + throws TorqueException + { + return ${peerImplGetter}().retrieveByObjectKeys(pks); + } + + /** + * Retrieve multiple objects by pk. + * + * @param pks List of primary keys + * @param con the connection to use + * + * @return the list of matching objects, not null. + * * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ + public static List<${dbObjectClassName}> retrieveByObjectKeys(Collection<ObjectKey> pks, Connection con) + throws TorqueException + { + return ${peerImplGetter}().retrieveByObjectKeys(pks, con); + } + + #if ($torqueGen.booleanOption("torque.om.retrieve.bypk.deprecated")) + /** + * Retrieve a multiple objects by pk. + * + * @param pks List of primary keys + * + * @return the list of matching objects, not null. + * + * @throws TorqueException Any exceptions caught during processing will be + * rethrown wrapped into a TorqueException. + * + * @deprecated This method has been renamed to retrieveByObjectKeys. + * This method will not be generated any more in future versions of Torque. + */ public static List<${dbObjectClassName}> retrieveByPKs(Collection<ObjectKey> pks) throws TorqueException { @@ -43,17 +122,24 @@ } /** - * Retrieve multiple objects by pk + * Retrieve multiple objects by pk. * * @param pks List of primary keys * @param con the connection to use + * + * @return the list of matching objects, not null. + * * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. + * + * @deprecated This method has been renamed to retrieveByObjectKeys. + * This method will not be generated any more in future versions of Torque. */ public static List<${dbObjectClassName}> retrieveByPKs(Collection<ObjectKey> pks, Connection con) throws TorqueException { - return ${peerImplGetter}().retrieveByPKs(pks,con); + return ${peerImplGetter}().retrieveByPKs(pks, con); } + #end #end \ No newline at end of file Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencedObject.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/fillReferencedObject.vm?rev=1706946&r1=1706945&r2=1706946&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencedObject.vm (original) +++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencedObject.vm Tue Oct 6 02:44:30 2015 @@ -178,7 +178,7 @@ #if ($referencesPrimaryKey) List<${fieldType}> referenced${fieldType}List - = ${foreignTablePeerClassName}.retrieveByPKs( + = ${foreignTablePeerClassName}.retrieveByObjectKeys( foreignKeyListChunk, connection); #else Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPKs.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/retrieveByPKs.vm?rev=1706946&r1=1706945&r2=1706946&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPKs.vm (original) +++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPKs.vm Tue Oct 6 02:44:30 2015 @@ -29,21 +29,28 @@ ## #set ( $primaryKeyColumnElements = $torqueGen.getChild("primary-keys").getChildren("column")) #if (!$primaryKeyColumnElements.isEmpty()) + #if ($primaryKeyColumnElements.size() == 1) + #set ( $columnElement = $primaryKeyColumnElements.get(0) ) + #set ( $columnFieldType = $columnElement.getAttribute("fieldObjectType")) /** - * Retrieve a multiple objects by pk + * Retrieve multiple objects by pk. + * + * @param pks List of primary keys. + * Entries in pks which do not match entries in the database are ignored. + * + * @return the list of matching objects, not null. * - * @param pks List of primary keys * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ - public List<${dbObjectClassName}> retrieveByPKs(Collection<ObjectKey> pks) + public List<${dbObjectClassName}> retrieveByTypedPKs(Collection<${columnFieldType}> pks) throws TorqueException { Connection connection = null; try { connection = Transaction.begin(${peerClassName}.DATABASE_NAME); - List<${dbObjectClassName}> result = retrieveByPKs(pks, connection); + List<${dbObjectClassName}> result = retrieveByTypedPKs(pks, connection); Transaction.commit(connection); connection = null; return result; @@ -58,14 +65,82 @@ } /** - * Retrieve multiple objects by pk + * Retrieve multiple objects by pk. * - * @param pks List of primary keys + * @param pks List of primary keys. + * Entries in pks which do not match entries in the database are ignored. * @param dbcon the connection to use + * + * @return the list of matching objects, not null. + * * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ - public List<${dbObjectClassName}> retrieveByPKs( + public List<${dbObjectClassName}> retrieveByTypedPKs( + Collection<${columnFieldType}> pks, + Connection dbcon) + throws TorqueException + { + if (pks == null || pks.size() == 0) + { + return new ArrayList<${dbObjectClassName}>(); + } + List<ObjectKey> objectKeyList = new ArrayList<ObjectKey>(); + for (${columnFieldType} pk : pks) + { + objectKeyList.add(SimpleKey.keyFor(pk)); + } + Criteria criteria = buildCriteria(objectKeyList); + List<${dbObjectClassName}> result = doSelect(criteria, dbcon); + return result; + } + + #end + /** + * Retrieve multiple objects by pk. + * + * @param pks List of primary keys. + * Entries in pks which do not match entries in the database are ignored. + * + * @return the list of matching objects, not null. + * + * @throws TorqueException Any exceptions caught during processing will be + * rethrown wrapped into a TorqueException. + */ + public List<${dbObjectClassName}> retrieveByObjectKeys(Collection<ObjectKey> pks) + throws TorqueException + { + Connection connection = null; + try + { + connection = Transaction.begin(${peerClassName}.DATABASE_NAME); + List<${dbObjectClassName}> result = retrieveByObjectKeys(pks, connection); + Transaction.commit(connection); + connection = null; + return result; + } + finally + { + if (connection != null) + { + Transaction.safeRollback(connection); + } + } + } + + /** + * Retrieve multiple objects by pk. + * + * @param pks List of primary keys. + * Entries in pks which do not match entries in the database are ignored. + * @param dbcon the connection to use + * + * @return the list of matching objects, not null. + * + * @throws TorqueException Any exceptions caught during processing will be + * rethrown wrapped into a TorqueException. + */ + public List<${dbObjectClassName}> retrieveByObjectKeys( Collection<ObjectKey> pks, Connection dbcon) throws TorqueException @@ -79,4 +154,47 @@ return result; } + #if ($torqueGen.booleanOption("torque.om.retrieve.bypk.deprecated")) + /** + * Retrieve a multiple objects by pk. + * + * @param pks List of primary keys. + * Entries in pks which do not match entries in the database are ignored. + * + * @return the list of matching objects, not null. + * + * @throws TorqueException Any exceptions caught during processing will be + * rethrown wrapped into a TorqueException. + * + * @deprecated This method has been renamed to retrieveByObjectKeys. + * This method will not be generated any more in future versions of Torque. + */ + public List<${dbObjectClassName}> retrieveByPKs(Collection<ObjectKey> pks) + throws TorqueException + { + return retrieveByObjectKeys(pks); + } + + /** + * Retrieve multiple objects by pk. + * + * @param pks List of primary keys. + * Entries in pks which do not match entries in the database are ignored. + * @param con the connection to use + * + * @return the list of matching objects, not null. + * + * @throws TorqueException Any exceptions caught during processing will be + * rethrown wrapped into a TorqueException. + * + * @deprecated This method has been renamed to retrieveByObjectKeys. + * This method will not be generated any more in future versions of Torque. + */ + public List<${dbObjectClassName}> retrieveByPKs(Collection<ObjectKey> pks, Connection con) + throws TorqueException + { + return retrieveByObjectKeys(pks, con); + } + + #end #end \ No newline at end of file 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=1706946&r1=1706945&r2=1706946&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 Tue Oct 6 02:44:30 2015 @@ -20,6 +20,7 @@ package org.apache.torque.generated.peer */ import java.math.BigDecimal; +import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -27,6 +28,7 @@ import org.apache.torque.BaseDatabaseTes import org.apache.torque.NoRowsException; import org.apache.torque.om.NumberKey; import org.apache.torque.om.ObjectKey; +import org.apache.torque.om.SimpleKey; import org.apache.torque.test.dbobject.Author; import org.apache.torque.test.peer.AuthorPeer; import org.apache.torque.test.peer.MultiPkPeer; @@ -136,4 +138,40 @@ public class RetrieveByPkTest extends Ba } } // TODO test retrieveByPks + + /** + * Tests the retrieveByObjectKeys method + * + * @throws Exception if the test fails + */ + public void testRetrieveByObjectKeys() throws Exception + { + List<ObjectKey> objectKeys = new ArrayList<ObjectKey>(); + objectKeys.add(authorList.get(1).getPrimaryKey()); + objectKeys.add(authorList.get(2).getPrimaryKey()); + objectKeys.add(SimpleKey.keyFor(-5L)); + + List<Author> result = AuthorPeer.retrieveByObjectKeys(objectKeys); + assertEquals(2, result.size()); + assertTrue("authorList should contain author 1", result.contains(authorList.get(1))); + assertTrue("authorList should contain author 2", result.contains(authorList.get(2))); + } + + /** + * Tests the retrieveByObjectKeys method + * + * @throws Exception if the test fails + */ + public void testRetrieveByTypedKeys() throws Exception + { + List<Integer> typedKeys = new ArrayList<Integer>(); + typedKeys.add(authorList.get(1).getAuthorId()); + typedKeys.add(authorList.get(2).getAuthorId()); + typedKeys.add(-5); + + List<Author> result = AuthorPeer.retrieveByTypedPKs(typedKeys); + assertEquals(2, result.size()); + assertTrue("authorList should contain author 1", result.contains(authorList.get(1))); + assertTrue("authorList should contain author 2", result.contains(authorList.get(2))); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: torque-dev-unsubscr...@db.apache.org For additional commands, e-mail: torque-dev-h...@db.apache.org