Jakarta Torque - PPT

Download Report

Transcript Jakarta Torque - PPT

Jakarta - Torque
CIS 764 Presentation
Deepti Gupta
Overview







Object Relational Mapping
Torque overview
Configuring Torque
Invoking Torque
Sample application
Conclusion
References
Object Relational Mapping


The process of transforming between object
and relational modeling approaches.
Technique of mapping relational rows from a
relational database to objects in memory
where they can be manipulated by an objectoriented program.
Jakarta - Torque


Torque is a persistence layer.
Torque consists of two main components:


Generator - The Torque generator uses a single
XML database schema file to generate the SQL for
the target database and Torque's Peer-based
object relational model.
Runtime - The Torque runtime is required in
order to compile and use the classes produced by
the generator.
Supported RDBMS
RDBMS
Driver
MS Access
sun.jdbc.odbc.JdbcOdbcDriver
MS SQL
com.microsoft.jdbc.sqlserver.SQLServerDriver
MySQL
org.gjt.mm.mysql.Driver
Oracle
oracle.jdbc.driver.OracleDriver
Configuring Torque

3 configuration files



Torque Generator properties – build.properties
Torque database schema – torque-schema.xml
Torque runtime properties – torque.properties
1. Build.properties
Property
Description
Basic Properties
torque.project
The name of the project Torque will generate code for.
torque.database
The target database.
Database Settings
torque.database.createUrl
The JDBC URL that Torque can use to create and drop
databases if instructed to do so.
torque.database.driver
The JDBC database driver to use when connecting to your
database.
torque.database.user
The administrative username that has sufficient privileges
to create and drop databases and tables that Torque
executes at build time.
torque.database.password
The administrative password for the supplied username.
2. Database Schema
<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="author_id"
required="true"
type="INTEGER"
description="Foreign Key Author"/>
<foreign-key foreignTable="author">
<reference
local="author_id"
foreign="author_id"/>
</foreign-key>
</table>
</database>
3. Torque.properties

torque.database.default = bookstore

torque.bookstore.connection.driver = oracle.jdbc.driver.OracleDriver

torque.bookstore.connection.url=
jdbc:oracle:thin:@oracle.cis.ksu.edu:1521:oracle

torque.bookstore.connection.user = user

torque.bookstore.connection.password = password

log4j.rootCategory = DEBUG

log4j.appender.default = org.apache.log4j.FileAppender

log4j.appender.default.file = ./torque.log
Invoking Torque

Generating the object model
Use Ant along with the build.xml file to generate the object
model

Creating the SQL databases and tables
The command ant create-db can be used for this purpose

Generating HTML docs
The command ant doc can be used for this purpose
Docs generated by Torque
Torque Object Map
Torque generates four classes for each table defined in the database
schema.

E.g. Book






BaseBook.java
: Torque-generated logic
BaseBookPeer.java : Torque-generated logic
Book.java
BookPeer.java
: Empty sub classes
: Empty sub classes
Adding Functionality to the Object Model
The sub classes are used to extend the object model.
E.g. : Add toString() functions to the model
Torque Application

Inserting rows
Book effective = new Book();
effective.setTitle(“ Effective Java ");
effective.save();

Selecting rows
Criteria crit = new Criteria();
crit.add(BookPeer.title, "Effective Java");
List books = BookPeer.doSelect(crit);

Deleting rows
Criteria crit = new Criteria();
crit.add(BookPeer.title, "Effective Java");
BookPeer.doDelete(crit);
Conclusion



Learning curve
No SQL statements
Relational database access in object
oriented way
References

http://db.apache.org/torque/index.html
-

User Guide
Torque tutorial
Downloads
Miscellaneous Information
http://www.developer.com/java/other/article.php/109
36_1457081_1 - Using the Torque Object Mapper