You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by tf...@apache.org on 2007/05/05 13:42:09 UTC
svn commit: r535508 - in /db/torque:
runtime/trunk/src/java/org/apache/torque/util/BasePeer.java
site/trunk/xdocs/changes.xml templates/trunk/src/templates/om/Peer.vm
test/trunk/test-project/src/java/org/apache/torque/DataTest.java
Author: tfischer
Date: Sat May 5 04:42:07 2007
New Revision: 535508
URL: http://svn.apache.org/viewvc?view=rev&rev=535508
Log:
Fixed the problem withSomePeer.doDelete(Criteria) if the criteria contains a join. Fixes TORQUE-93.
Modified:
db/torque/runtime/trunk/src/java/org/apache/torque/util/BasePeer.java
db/torque/site/trunk/xdocs/changes.xml
db/torque/templates/trunk/src/templates/om/Peer.vm
db/torque/test/trunk/test-project/src/java/org/apache/torque/DataTest.java
Modified: db/torque/runtime/trunk/src/java/org/apache/torque/util/BasePeer.java
URL: http://svn.apache.org/viewvc/db/torque/runtime/trunk/src/java/org/apache/torque/util/BasePeer.java?view=diff&rev=535508&r1=535507&r2=535508
==============================================================================
--- db/torque/runtime/trunk/src/java/org/apache/torque/util/BasePeer.java (original)
+++ db/torque/runtime/trunk/src/java/org/apache/torque/util/BasePeer.java Sat May 5 04:42:07 2007
@@ -303,16 +303,37 @@
* @param criteria The criteria to use.
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
+ * @deprecated This method causes unexpected results when joins are used.
+ * Please use doDelete(Criteria, String).
*/
public static void doDelete(Criteria criteria) throws TorqueException
{
+ doDelete(criteria, (String) null);
+ }
+
+ /**
+ * Method to perform deletes based on values and keys in a
+ * Criteria.
+ * This method is protected because it may cause ambiguity between
+ * doDelete(Criteria,Connection) and this method. It will be made public
+ * once doDelete(Criteria, Connection) is removed.
+ *
+ * @param criteria The criteria to use.
+ * @param tableName the name of the table to delete records from.
+ * If set to null, the name of the table(s) can be extracted from
+ * the criteria, but this can cause unexpected results.
+ * @throws TorqueException Any exceptions caught during processing will be
+ * rethrown wrapped into a TorqueException.
+ */
+ protected static void doDelete(Criteria criteria, String tableName) throws TorqueException
+ {
Connection con = null;
try
{
con = Transaction.beginOptional(
criteria.getDbName(),
criteria.isUseTransaction());
- doDelete(criteria, con);
+ doDelete(criteria, tableName, con);
Transaction.commit(con);
}
catch (TorqueException e)
@@ -329,10 +350,29 @@
* @param con A Connection.
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
+ * @deprecated This method causes unexpected results when joins are used.
+ * Please use doDelete(Criteria, String, Connection).
*/
public static void doDelete(Criteria criteria, Connection con)
throws TorqueException
{
+ doDelete(criteria, null, con);
+ }
+
+ /**
+ * Method to perform deletes based on values and keys in a Criteria.
+ *
+ * @param criteria The criteria to use.
+ * @param tableName the name of the table to delete records from.
+ * If set to null, the name of the table(s) can be extracted from
+ * the criteria, but this can cause unexpected results.
+ * @param con A Connection.
+ * @throws TorqueException Any exceptions caught during processing will be
+ * rethrown wrapped into a TorqueException.
+ */
+ public static void doDelete(Criteria criteria, String tableName, Connection con)
+ throws TorqueException
+ {
String dbName = criteria.getDbName();
final DatabaseMap dbMap = Torque.getDatabaseMap(dbName);
@@ -370,7 +410,16 @@
}
};
- Set tables = SQLBuilder.getTableSet(criteria, tc);
+ Set tables;
+ if (tableName == null)
+ {
+ tables = SQLBuilder.getTableSet(criteria, tc);
+ }
+ else
+ {
+ tables = new HashSet(1);
+ tables.add(tableName);
+ }
try
{
Modified: db/torque/site/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/db/torque/site/trunk/xdocs/changes.xml?view=diff&rev=535508&r1=535507&r2=535508
==============================================================================
--- db/torque/site/trunk/xdocs/changes.xml (original)
+++ db/torque/site/trunk/xdocs/changes.xml Sat May 5 04:42:07 2007
@@ -31,6 +31,10 @@
<body>
<release version="3.3-RC3" date="in SVN">
+ <action type="fix" dev="tfischer" issue="TORQUE-93">
+ Fixed problem with somePeer.doDelete(Criteria) if criteria contains
+ a join.
+ </action>
<action type="add" dev="gmonroe">
Added torque.beanExtendsClass property that defines a fully qualified
class name (e.g. org.apache.struts.action.ActionForm) that the base
Modified: db/torque/templates/trunk/src/templates/om/Peer.vm
URL: http://svn.apache.org/viewvc/db/torque/templates/trunk/src/templates/om/Peer.vm?view=diff&rev=535508&r1=535507&r2=535508
==============================================================================
--- db/torque/templates/trunk/src/templates/om/Peer.vm (original)
+++ db/torque/templates/trunk/src/templates/om/Peer.vm Sat May 5 04:42:07 2007
@@ -653,11 +653,11 @@
if (con == null)
{
- BasePeer.doDelete(criteria);
+ BasePeer.doDelete(criteria, TABLE_NAME);
}
else
{
- BasePeer.doDelete(criteria, con);
+ BasePeer.doDelete(criteria, TABLE_NAME, con);
}
}
Modified: db/torque/test/trunk/test-project/src/java/org/apache/torque/DataTest.java
URL: http://svn.apache.org/viewvc/db/torque/test/trunk/test-project/src/java/org/apache/torque/DataTest.java?view=diff&rev=535508&r1=535507&r2=535508
==============================================================================
--- db/torque/test/trunk/test-project/src/java/org/apache/torque/DataTest.java (original)
+++ db/torque/test/trunk/test-project/src/java/org/apache/torque/DataTest.java Sat May 5 04:42:07 2007
@@ -669,20 +669,40 @@
book.save();
criteria.clear();
- criteria.add(AuthorPeer.AUTHOR_ID, author.getAuthorId());
+ criteria.add(BookPeer.AUTHOR_ID, author.getAuthorId());
criteria.addJoin(AuthorPeer.AUTHOR_ID, BookPeer.AUTHOR_ID);
- // The following where clause is nit necessary from a sql pointof view.
+ // The following where clause is not necessary from a sql point of view.
// However, it adds a second table to the where clauses of the
// criteria, so that it cannot be determined from the criteria alone
- // which table should be deleted from.
- criteria.add(BookPeer.AUTHOR_ID, book.getAuthorId());
- AuthorPeer.doDelete(criteria);
+ // which table should be deleted from. So this may cause data to
+ // disappear from both tables (TORQUE-93)
+ criteria.add(AuthorPeer.AUTHOR_ID, book.getAuthorId());
+ BookPeer.doDelete(criteria);
authorResult = AuthorPeer.doSelect(new Criteria());
bookResult = BookPeer.doSelect(new Criteria());
assertTrue("deleted not enough records",
- authorResult.size() == 0);
+ bookResult.size() == 0);
assertTrue("delete also deleted objects in joined table",
- bookResult.size() == 1);
+ authorResult.size() == 1);
+
+ // recreate book, test whether deletes using joins work
+ book = new Book();
+ book.setTitle("title");
+ book.setAuthor(author);
+ book.setIsbn("ISBN");
+ book.save();
+
+ criteria.clear();
+ criteria.addJoin(BookPeer.AUTHOR_ID, AuthorPeer.AUTHOR_ID);
+ criteria.add(AuthorPeer.NAME, author.getName());
+ BookPeer.doDelete(criteria);
+
+ authorResult = AuthorPeer.doSelect(new Criteria());
+ bookResult = BookPeer.doSelect(new Criteria());
+ assertTrue("deleted not enough records",
+ bookResult.size() == 0);
+ assertTrue("delete also deleted objects in joined table",
+ authorResult.size() == 1);
}
/**
@@ -739,12 +759,12 @@
// test double functions in select columns
Criteria criteria = new Criteria();
criteria.addSelectColumn("count(distinct(" + BookPeer.BOOK_ID + "))");
- List result = BookPeer.doSelectVillageRecords(criteria);
+ BookPeer.doSelectVillageRecords(criteria);
// test qualifiers in function in select columns
criteria = new Criteria();
criteria.addSelectColumn("count(distinct " + BookPeer.BOOK_ID + ")");
- result = BookPeer.doSelectVillageRecords(criteria);
+ BookPeer.doSelectVillageRecords(criteria);
}
/**
@@ -755,7 +775,7 @@
public void testNullConnection() throws Exception
{
Criteria criteria = new Criteria();
- List result = BookPeer.doSelectVillageRecords(criteria, null);
+ BookPeer.doSelectVillageRecords(criteria, null);
criteria = new Criteria();
criteria.add(BookPeer.BOOK_ID, (Long) null, Criteria.NOT_EQUAL);
@@ -1957,13 +1977,9 @@
protected void cleanBookstore() throws Exception
{
Criteria criteria = new Criteria();
- criteria.add(BookPeer.BOOK_ID, (Long) null, Criteria.NOT_EQUAL);
BookPeer.doDelete(criteria);
criteria.clear();
- criteria.add(
- AuthorPeer.AUTHOR_ID,
- (Long) null, Criteria.NOT_EQUAL);
AuthorPeer.doDelete(criteria);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org