From torque-dev-return-11172-apmail-db-torque-dev-archive=db.apache.org@db.apache.org Sat Jun 2 14:34:09 2012
Return-Path:
A correct ant build file is very important. - This enablee the Torque generator to generate all of + This enables the Torque generator to generate all of the required sources and SQL for your specific database. If you experience problems later in this tutorial, it would be wise to double-check this file. Modified: db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step1-maven.xml URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step1-maven.xml?rev=1345513&r1=1345512&r2=1345513&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step1-maven.xml (original) +++ db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step1-maven.xml Sat Jun 2 14:33:25 2012 @@ -31,11 +31,11 @@
The following section outlines the necessary steps to - configure a Torque-based ORM project using maven. + configure a Torque-based ORM project using Maven. For this, you need to create maven's pom file (the maven project descriptor file) which describes your project. - It is recommended to run the tutorial against a mysql datanase, as - all of the explanation in this tutorial assumes that you use mysql. + It is recommended to run the tutorial against a Mysql database, as + all of the explanation in this tutorial assumes that you use Mysql.
@@ -55,7 +55,7 @@ use the following template and save it as pom.xml in the project's base directory. Then edit it to reflect your specific needs ( - typically you need to change the database urls, + typically you need to change the database URLs, the database host, the database user and password):
@@ -87,6 +87,14 @@Another common mistake is to forget that XML is case-sensitive. All of the elements and - attributes must be specified according to the TODO - DTD + attributes must be specified according to the + XML Schema for the database schema. In addition, you should - include the XML declaration and DTD specification in - your database schema file. + include the XML declaration and XML schema specification + in your database schema file. This helps to detect errors in the schema file easily.
For additional information on the XML elements and attributes, please refer to the - - Torque Schema Reference. + + Torque Schema XSD.
Modified: db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step3-ant.xml URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step3-ant.xml?rev=1345513&r1=1345512&r2=1345513&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step3-ant.xml (original) +++ db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step3-ant.xml Sat Jun 2 14:33:25 2012 @@ -49,13 +49,15 @@- The object model consists of four classes for each + The object model consists of eight classes for each table in your schema. For example, the author table, defined in this tutorial, will result in the following four classes: - Author, AuthorPeer, - BaseAuthor, and BaseAuthorPeer (a - discussion on the use of these classes is deferred + Author, AuthorPeer, + AuthorPeerImpl, AuthorPeerRecordMapper, + BaseAuthor, BaseAuthorPeer, + BaseAuthorPeerImpl, and BaseAuthorPeerRecordMapper. + (a discussion on the use of these classes is deferred until we write our sample application).
@@ -139,8 +141,8 @@ ant generate to create tables. Alternatively, you can just use the root user to create the database tables. In both cases, check again that the username, password and url - of your mysql database is correct in the build.xml, and type the following command in - the top-level directory of your project: + of your mysql database is correct in the build.xml, + and type the following command in the top-level directory of your project:
Modified: db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step3-maven.xml
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step3-maven.xml?rev=1345513&r1=1345512&r2=1345513&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step3-maven.xml (original)
+++ db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step3-maven.xml Sat Jun 2 14:33:25 2012
@@ -30,7 +30,7 @@
- With the configuration of maven and definition of the
+ With the configuration of Maven and definition of the
database schema completed, you can now
generate the object model to support your database,
and create the database tables.
@@ -49,13 +49,15 @@
- The object model consists of four classes for each
+ The object model consists of eight classes for each
table in your schema. For example, the
author table, defined in this tutorial,
will result in the following four classes:
- Author, AuthorPeer,
- BaseAuthor, and BaseAuthorPeer (a
- discussion on the use of these classes is deferred
+ Author, AuthorPeer,
+ AuthorPeerImpl, AuthorPeerRecordMapper,
+ BaseAuthor, BaseAuthorPeer,
+ BaseAuthorPeerImpl, and BaseAuthorPeerRecordMapper.
+ (a discussion on the use of these classes is deferred
until we write our sample application).
A successful build will be indicated by the
- BUILD SUCCESSFUL message.
+ BUILD SUCCESSFUL message.
@@ -137,7 +139,7 @@ mvn generate-sources
The tutorial uses mysql as database, and the pom templates
- gicen in step 1 already include a mysql dependency,
+ given in step 1 already include a mysql dependency,
so the pom xml in the tutorial project can remain unchanged.
Success will be indicated by the message
- 8 of 8 SQL statements executed successfully
- BUILD SUCCESSFUL. You can also validate
- this by checking your database. For example, the
+ 8 of 10 SQL statements executed successfully
+ BUILD SUCCESSFUL.
+ (Torque assumes that you already have defined a database,
+ and performs a drop before attempting recreation.
+ The statements dropping the foreign keys fail because
+ the table does not exist, but this is not a problem
+ for creating the tables. If you re-run the command,
+ all 10 statements will execute successfully.)
+ You can also validate that the tables were created
+ by checking your database. For example, the
bookstore-schema.xml defined in this
tutorial should have created the following tables:
author, book,
Modified: db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step4.xml
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step4.xml?rev=1345513&r1=1345512&r2=1345513&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step4.xml (original)
+++ db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step4.xml Sat Jun 2 14:33:25 2012
@@ -47,10 +47,7 @@
executing the object model code generated by Torque.
The runtime properties control database
parameters such as drivers, usernames, and
- passwords. These properties can be saved in any
- file because your application must explicitly
- initialize Torque (as you'll see later in this
- tutorial).
+ passwords.
Modified: db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step5.xml
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step5.xml?rev=1345513&r1=1345512&r2=1345513&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step5.xml (original)
+++ db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step5.xml Sat Jun 2 14:33:25 2012
@@ -37,44 +37,56 @@ the power of Torque.
As mentioned earlier, when Torque created your object
-model, it created four Java classes for each table
+model, it created eight Java classes for each table
defined in your database schema. For example, the
book table, defined in the database schema
presented earlier, will result in the following classes:
-Book, BookPeer, BaseBook, and
-BaseBookPeer.
+Book, BookPeer,
+BookPeerImpl, BookRecordMapper,
+BaseBook, BaseBookPeer,
+BaseBookPeerImpl, and BaseBookRecordMapper.
-Book and BookPeer are subclasses of
-BaseBook and BaseBookPeer
-respectively. The two Base classes (BaseBook
-and BaseBookPeer) contain Torque-generated
+Book, BookPeer, BookPeerImpl and
+BookRecordMapper are subclasses of
+BaseBook, BaseBookPeer
+BaseBookPeerImpl and BaseBookRecordMapper, respectively.
+BookPeer and BaseBookPeer are simply static wrappers
+of BookPeerImpl and BaseBookPeerImpl.
+The four Base classes (Base*) contain Torque-generated
logic and should not be modified because Torque
will overwrite your changes if you happen to generate
-your object model again. Any
-business logic that you might want to add should be
-placed in the Book and BookPeer
-classes. This is covered later in the tutorial.
+your object model again.
+Any business logic that you might want to add should be
+placed in the Book and either BookPeerImpl
+or BookPeer classes (this is covered later in the tutorial).
You might be asking yourself, what is the difference
-between the Peer classes (BookPeer and
-BaseBookPeer) and their counterparts
-(Book and BaseBook), also known as
-Data Objects? The Peer classes "wrap" their
-associated database tables and provide static methods to
-manipulate those tables such as doSelect and
-doDelete. Data Objects,
-on the other hand, "wrap" individual rows
-within those tables and provide getters/mutators for each
+between the BookPeer, BookPeerImpl, BookRecordMapper and Book classes?
+The BookPeer and BookPeerImpl classes provide the same functionality,
+with the difference that the Peer classes provide static access to
+methods for manipulating tables while the PeerImpl classes
+contain the implementation of those methods. This allows
+the implementation to be exchanged after compilation
+(e.g. for testing purposes).
+If you do not need to exchange the implementation of the Peer classes,
+you need not bother about the PeerImpl classes
+and simply use the Peer classes.
+The Book class (also known as "Data Object" class), "wrap" individual rows
+within the tables and provide getters/mutators for each
column defined in those tables as well as a
-save method. Both Peer and Data Objects have a
-one-to-one mapping to a table defined in your database
+save method.
+The RecordMapper classes map between JDBC Rows and the Data objects
+(chances are that you never use these directly).
+So the two of the eight classes you will probably be working with are the
+Peer classes (wrapping the table) and Data Objects (wrapping a table row)
+These have a one-to-one mapping to a table defined in your database
schema. For a more in-depth discussion on Peers and
Data Objects, refer to the
-Runtime relevant classes documentation.
+Runtime relevant classes documentation.
An example of adding logic to both the Peer and Data
Objects is presented later in the tutorial.
To select a specific Book from the table,
create a Criteria object (or just reuse the
- previous one) and use the add method to
- specify some criteria. Specifying criteria is
+ previous one) and use the where methods to specify
+ a condition.
+ Specifying a condition is
simply a matter of choosing a column (defined as
static constants in your Peer class) and some value
you want to match. Thus, selecting a book with the
- following ISBN, 0-618-12902-2, is as
+ following ISBN, 0-618-12902-2, is as
simple as:
+ As an alternative, for selects using joins,
+ there are the following
protected methods in the BasePeer
classes whose tables contain foreign-keys:
doSelectJoinTable where
@@ -292,18 +314,19 @@ for (Book book : books)
For example, changing the author of the - Effective Java book created earlier is + Effective Java book created earlier is as simple as:
@@ -395,34 +419,6 @@ PublisherPeer.doDelete(addison); author and publisher tables. - -| The Gory Details (not for the faint) |
|---|
| - It should also be noted that - doDelete does not construct its - WHERE clause in a similar manner as the - doSelect method. doDelete - processes Criteria in a more - primitive fashion. Specifically, - Criteria assembled using the - and and or methods (not - covered in this tutorial) are effectively - ignored. In addition, passing an empty - Criteria to doDelete will - not delete all of the rows from a table. In - summary, you cannot assume that a - Criteria object which successfully - selects rows from a table via - doSelect will delete those rows if - passed to doDelete. In the future, - doDelete may be modified to be - consistent in the handling of - Criteria objects. - | -
This section will provide examples of adding functionality to both the Peer and Data Object - classes. As you may recall, Torque generated four + classes. As you may recall, Torque generated eight classes for each table defined in the database - schema. Two of these classes (the Base Data Object - and Base Peer class) contain Torque-generated logic - while the other two are empty subclasses that you + schema. Four of these classes (the Base classes) + contain Torque-generated logic + while the other ones are empty subclasses that you can use to include business logic. By now, you should have a decent understanding of the type of logic that might be added to these classes. Keep in @@ -509,7 +505,7 @@ public class Publisher extends BasePubli The next change that we'll make is to the Peer classes. For convenience (and based on the suggestion in the - Reading + Reading from the Database Reference) we'll add doSelectAll methods which will return a List of all the Data Objects in a table. The following are the modified @@ -524,7 +520,7 @@ package org.apache.torque.tutorial.om; import java.util.List; import org.apache.torque.TorqueException; -import org.apache.torque.util.Criteria; +import org.apache.torque.criteria.Criteria; public class BookPeer extends BaseBookPeer { @@ -540,7 +536,7 @@ package org.apache.torque.tutorial.om; import java.util.List; import org.apache.torque.TorqueException; -import org.apache.torque.util.Criteria; +import org.apache.torque.criteria.Criteria; public class AuthorPeer extends BaseAuthorPeer { @@ -556,7 +552,7 @@ package org.apache.torque.tutorial.om; import java.util.List; import org.apache.torque.TorqueException; -import org.apache.torque.util.Criteria; +import org.apache.torque.criteria.Criteria; public class PublisherPeer extends BasePublisherPeer { @@ -601,9 +597,11 @@ import java.io.InputStream; import java.util.List; import org.apache.commons.configuration.PropertiesConfiguration; -import org.apache.log4j.*; +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; import org.apache.torque.Torque; -import org.apache.torque.util.Criteria; +import org.apache.torque.criteria.Criteria; public class Bookstore { @@ -678,7 +676,7 @@ public class Bookstore */ System.out.println("Booklist (specific ISBN):\n"); Criteria crit = new Criteria(); - crit.add(BookPeer.ISBN, "0-201-63346-9"); + crit.where(BookPeer.ISBN, "0-201-63346-9"); booklist = BookPeer.doSelect(crit); printBooklist(booklist); @@ -701,12 +699,12 @@ public class Bookstore * specified criteria. */ crit = new Criteria(); - crit.add(BookPeer.ISBN, "0-618-12902-2"); + crit.where(BookPeer.ISBN, "0-618-12902-2"); BookPeer.doDelete(crit); crit = new Criteria(); - crit.add(BookPeer.ISBN, "0-201-63346-9"); - crit.add(BookPeer.TITLE, "TCP/IP Illustrated, Volume 1"); + crit.where(BookPeer.ISBN, "0-201-63346-9"); + crit.and(BookPeer.TITLE, "TCP/IP Illustrated, Volume 1"); BookPeer.doDelete(crit); /* Modified: db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step6-ant.xml URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step6-ant.xml?rev=1345513&r1=1345512&r2=1345513&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step6-ant.xml (original) +++ db/torque/torque4/trunk/torque-site/src/site/xdoc/tutorial/orm/step6-ant.xml Sat Jun 2 14:33:25 2012 @@ -40,7 +40,8 @@ in the top level directory of the project. Add all libraries from the lib dir of the torque-runtime binary distribution and also the torque-runtime-${version}.jar from its root directory. - Additionally copy the mysql driver jar from lib/ant to lib/runtime. + Additionally copy the mysql driver jar and the log4j jar + from lib/ant to lib/runtime.
@@ -96,10 +97,10 @@ ant