150 likes | 306 Views
Jakarta - Torque. CIS 764 Presentation Deepti Gupta. Overview. Object Relational Mapping Torque overview Configuring Torque Invoking Torque Sample application Conclusion References. Object Relational Mapping.
E N D
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 object-oriented 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.
Configuring Torque • 3 configuration files • Torque Generator properties – build.properties • Torque database schema –torque-schema.xml • Torque runtime properties – torque.properties
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
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 : Empty sub classes • BookPeer.java : 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/10936_1457081_1 - Using the Torque Object Mapper