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
* 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.
* 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.
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!
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:
GRANT alter,create,delete,drop,insert,select,update ON *.* TO email@example.com 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.
2. Copy the torque packages and the mysql driver package to the howto directory from where you downloded them to.
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.
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.
unzip ../mysql\ java\ driver.zip
5. Copy the MySQL driver jar file to the torque generator's lib directory.
5.2 Generating the OM
cp mysql-connector-java-3.0.9-stable/mysql-connector-java-3.0.9-stable-bin.jar ../torque-gen-3.1/lib/
1. Copy the torque runtime jar file torque.jar from the torque runtime lib directory to the torque generator lib directory.
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.
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.
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.
Add the following database schema to the file, save it and exit from your editor.
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE database SYSTEM
<table name="book" description="Book Table">
description="Foreign Key Publisher"/>
description="Foreign Key Author"/>
<table name="publisher" description="Publisher Table">
<table name="author" description="Author Table">
5. Edit the id-table-schema.xml file in the schema directory, changing the database name to:
6. Now generate the object model using Apache Ant by running the following commands, one after the other from within the torque generator directory.
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:
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.
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:
To be continued...