1. Software Used
* MySQL version 4.0.12 standard
* MySQL Driver version 3.0.9
* Apache Ant version 1.6.2
* Apache Torque version 3.1
* Apache Velocity version 1.4
* Apache Tomcat version 5.0.1
* Red Hat Linux version 9
2. Prerequisites and Outcomes
2.1 Prerequisites
* You should have sufficient programming skills using the Java Language. You probably wouldn't be reading this if you didn't! ;o)
* You should have already installed Apache Ant, Apache Tomcat and MySQL and be comfortable using these packages. If not, now would be a good time to go install them and get aquainted with them.
* You should be comfortable with extracting archives, copying and renaming files, creating files and using a text editor to edit files.
* We are going to use java servlet technology for the web application, you should have some general background on servlets and have the servlet API or the J2EE API.
2.2 Outcomes
* See how useful Apache Torque is and enjoy the benefits of generating an object model with it.
* See how powerful the Apache Velocity template engine is and use it to generate dynamic content in a web application.
* See how easy it is to generate dynamic content from a database using the powerful combination of Apache Torque and Apache Velocity.
3. Introduction
Most interactive web applications today use a databse on the backend. Using Java Servlets is a powerful way to get data from the database into the display and vice versa. A combination of Servlets and JDBC has one diadvantage though, you are still required to write SQL code. This is not a problem if the database is small but as soon as it becomes large and complex, it becomes a pain to write all the SQL code and even more so when that code has to be debugged. Apache Torque is a tool that maps relational databases to an object model. This eliminates the need to write SQL code, and instead you can use simple Java objects to perform complex database transactions. This has two benefits, programmers that are not fluent in SQL can simply focus on Java, and further complex SQL is avoided completly, thus only Java code needs to be debugged.
Using the Model - View - Controller pattern, developers often use Java Server Pages on the back end. While JSP is powerful and has numerous supporters, it has one fundamental flaw. It combines java code with HTML code. And while it is not possible to completely separate Java and HTML, the more separation one can achieve, the better. This is where Velocity has a major advantage: the code contained in the HTML is minimal and simplistic, this is what you need when your page designers are not programmers. The programmers focus on the Java Code, and the page designers focus on the HTML code. They never get in each other's way! A combination of Apache Velocity and Apache Torque is a very powerful but easy to use solution for developing complex dynamic web applications. I hope that this how-to will prove this!
4. MySQL
The first thing that we'll want to do is to set up a user account for Apache Torque, it really is a bad idea to use the MySQL root account. To do this, log into MySQL as root and execute the command:
Code:
GRANT alter,create,delete,drop,insert,select,update ON *.* TO torque@127.0.0.1 IDENTIFIED BY 'torque';
Log out from root and then try to log into the torque account:
Enter torque as the password. If you get in, everything is set, if not you probably had a typo in the commands above. Try it again.
5. Generating the OM with Torque
5.1 Extracting the packages
1. Create a directory to work in. I used howto. Also create a temporary directory for the mysql driver.
Code:
mkdir howto
mkdir howto/driver
2. Copy the torque packages and the mysql driver package to the howto directory from where you downloded them to.
Code:
cd howto
cp ~/torque-3.1.tar.gz .
cp ~/torque-gen-3.1.tar.gz .
cp ~/mysql java driver.zip .
3. Extract the torque runtime and generator packages.
Code:
tar xzvf torque-3.1.tar.gz
tar xzvf torque-gen-3.1.tar.gz
4. Now extract the mysql driver package in the driver directory.
Code:
cd driver
unzip ../mysql\ java\ driver.zip
5. Copy the MySQL driver jar file to the torque generator's lib directory.
Code:
cp mysql-connector-java-3.0.9-stable/mysql-connector-java-3.0.9-stable-bin.jar ../torque-gen-3.1/lib/
5.2 Generating the OM
1. Copy the torque runtime jar file torque.jar from the torque runtime lib directory to the torque generator lib directory.
Code:
cp ../torque-3.1/lib/torque.jar ../torque-gen-3.1/lib
2. Change into the torque generator directory and edit the build.properties file with your favourite text editor. I used vi.
Code:
cd ../torque-gen-3.1
vi build.properties
3. Change the properties in the file so that they match the ones given below. If a property is not given below, you can leave it "as is" in the file.
Code:
torque.project = bookstore
torque.database = mysql
torque.targetPackage = com.kazmier.om
torque.database.createUrl = jdbc:mysql://127.0.0.1/mysql
torque.database.buildUrl = jdbc:mysql://127.0.0.1/bookstore
torque.database.url = jdbc:mysql://127.0.0.1/bookstore
torque.database.driver = org.gjt.mm.mysql.Driver
torque.database.user = torque
torque.database.password = torque
torque.database.host = 127.0.0.1
4. Create the database schema in a file named bookstore-schema.xml in the schema subdirectory in the torque generator directory.
Code:
touch schema/bookstore-schema.xml
Add the following database schema to the file, save it and exit from your editor.
Code:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE database SYSTEM
"http://db.apache.org/torque/dtd/database_3_1.dtd">
<database
name="bookstore"
defaultIdMethod="idbroker">
<table name="book" description="Book Table">
<column
name="book_id"
required="true"
primaryKey="true"
type="INTEGER"
description="Book Id"/>
<column
name="title"
required="true"
type="VARCHAR"
size="255"
description="Book Title"/>
<column
name="isbn"
required="true"
type="VARCHAR"
size="24"
javaName="ISBN"
description="ISBN Number"/>
<column
name="publisher_id"
required="true"
type="INTEGER"
description="Foreign Key Publisher"/>
<column
name="author_id"
required="true"
type="INTEGER"
description="Foreign Key Author"/>
<foreign-key foreignTable="publisher">
<reference
local="publisher_id"
foreign="publisher_id"/>
</foreign-key>
<foreign-key foreignTable="author">
<reference
local="author_id"
foreign="author_id"/>
</foreign-key>
</table>
<table name="publisher" description="Publisher Table">
<column
name="publisher_id"
required="true"
primaryKey="true"
type="INTEGER"
description="Publisher Id"/>
<column
name="name"
required="true"
type="VARCHAR"
size="128"
description="Publisher Name"/>
</table>
<table name="author" description="Author Table">
<column
name="author_id"
required="true"
primaryKey="true"
type="INTEGER"
description="Author Id"/>
<column
name="first_name"
required="true"
type="VARCHAR"
size="128"
description="First Name"/>
<column
name="last_name"
required="true"
type="VARCHAR"
size="128"
description="Last Name"/>
</table>
</database>
5. Edit the id-table-schema.xml file in the schema directory, changing the database name to:
Code:
<database name="bookstore">
6. Now generate the object model using Apache Ant by running the following commands, one after the other from within the torque generator directory.
Code:
ant -f build-torque.xml
ant -f build-torque.xml create-db
ant -f build-torque.xml id-table-init-sql
ant -f build-torque.xml insert-sql
ant -f build-torque.xml compile
ant -f build-torque.xml javadocs
In each case above you should get a number of output messages and finally a message like:
Code:
BUILD SUCCESSFUL
Total time: 15 seconds
You should also check that all the SQL statements execute successfully, the create-db and insert-sql targets will execute SQL. You should check for a message saying something like 11 of 11 SQL statements executed successfully.
7. Open the javadoc api for your object model in your favourite web browser.
Code:
konqueror ./src/javadoc/index.html
You should have two packages: com.kazmier.om and com.kazmier.om.map, with a number of classes in each. Of importance is to note that you will have four classes for each table in the database, for example the book entity:
Code:
Book
BookPeer
BaseBook
BaseBookPeer
To be continued...