Introduction
Apache Torque is a object-relational mapper for Java. In the other words, Torque lets you access and manipulate data in a relational database using Java objects. Unlike most other object-relational mappers, Torque does isn't use reflection to access user-provided classes, but it genera TES the necessary classes (including the Data Objects) from the XML schema describing the database layout. The XML file can either is written by hand or a starting point can is generated from an existing database. The XML schema can also is used to generate and execute a SQL script which creates all of the tables in the database.
Apache Torque is a object-relational mapper. Torque allows users to access or manipulate data in a relational database via Java object. Unlike most other object-relational mappers,torque access-user-provided classes do not use reflection, they use the necessary classes (including data Objects) generated by the XML schema.
Torque has the primary foci:it is a tool for Java code generation and, building on top of that, It provides Object-relati Onal Mapping (ORM) for database access in Java. These aspects is treated in different parts of this tutorial; The parts can be read independently for each of the other.
Torque has two uses: one is a tool generated as Java code, and the other is based on generated code that provides object-relational mapping (ORM) access to the database. These two aspects can be considered as different parts and treated independently. The ORM section contains database creation and read-write data. The code generation section represents how to use torque generator to generate custom code (O/R classes).
Or-mapping
The Torque ORM Tutorial consists of the following steps:
- Step 1:configuring The Torque generation process (for Ant or maven)
- Step 2:defining The database schema
- Step 3:invoking the Torque generator (for Ant or maven)
- Step 4:configuring the Torque Runtime
- Step 5:writing a Sample application
- Step 6:compiling and Running the Sample application (for Ant or maven)
Step One: Configure Torque generation Process
Use maven or Ant-configured dependencies and plugin. Dependencies are mainly torque runtime or DB driver (such as Mysql-connector-java). Plugin is mainly based on the template Production Code configuration
Step two: Define the database schema
The second configuration file to be edited is the database schema. The database schema in torque is an XML file that describes the structure of the databases. A file can define a database's table, column name and type, key and index of the table.
Step three: Call Torque generator
Once the Maven/ant configuration and database schema definitions have been completed, you can then produce the object model and create the table. The creation of the object model generates a Java source file that represents the structure of the database. With these classes we can manipulate the data in the database. In addition, Torque generates SQL to create database tables.
When torque creates an object model, it generates 8 types for each table: 4 with base prefixes and 4 without prefixes. A class with a base prefix cannot be modified (because each generation is overwritten), a class without a prefix is a subclass of the base prefix class, and the implementation of the parent class can be overridden.
Note:torque will assume that the database already exists, and in the execution of SQL, it will drop the corresponding data first.
Step four: Configure Torque runtime
Configuring the torque operating environment simply means configuring the runtime's connection information, such as Adapter,driver,url, username and password.
| Property
Description |
Torque.database.default |
Torque have the ability to use multiple databases. The specifies which database is used as the default. |
Torque.database.XXX.adapter |
Torque has the ability to deal with multiple database systems. The specifies the database adapter to use. |
Torque.dsfactory.XXX.factory |
The factory class is used to provide database connections. |
Torque.dsfactory.XXX.connection.driver |
The JDBC database driver to is connecting to your database. |
Torque.database.XXX.connection.url |
The URL that would be used to access your database. Torque ' s generated object model would perform all database operations using the this URL. This value should reflect the database name specified in your database schema file (see the database element ' s name attribute). |
Torque.database.XXX.connection.user |
The username that have sufficient privileges to access your database. This user does does require privileges to create and drop tables, unlike the user that is specified in Project.propert IES. |
Torque.database.XXX.connection.password |
The password for the specified username. |
A simple example:
Torque.database. default == = = = Jdbc:mysql://localhost:3306/bookstore Torque.dsfactory.bookstore.connection.user == password
Step five: Use the torque.
Generates the necessary class, database tables, and configuration of the running connection information. The next step is how to use it in the application.
PackageOrg.apache.torque.tutorial.om;ImportJava.io.InputStream;Importjava.util.List;Importorg.apache.commons.configuration.PropertiesConfiguration;ImportOrg.apache.log4j.BasicConfigurator;ImportOrg.apache.log4j.Level;ImportOrg.apache.log4j.Logger;ImportOrg.apache.torque.Torque;ImportOrg.apache.torque.criteria.Criteria; Public classbookstore{ Public Static voidMain (string[] args) {Try { //Initializing Loggingbasicconfigurator.configure (); Logger.getrootlogger (). SetLevel (Level.warn); //Initializing TorqueInputStream Torqueconfigstream= Bookstore.class. getResourceAsStream ("/torque.properties"); Propertiesconfiguration torqueconfiguration=Newpropertiesconfiguration (); Torqueconfiguration.load (Torqueconfigstream); Torque.init (torqueconfiguration); /** Creating new objects. These'll be inserted to your database * automatically when the Save method is called. */Publisher Addison=NewPublisher (); Addison.setname ("Addison Wesley Professional"); Addison.save (); Author Bloch=NewAuthor (); Bloch.setfirstname ("Joshua"); Bloch.setlastname ("Bloch"); Bloch.save (); /** An alternative method to inserting rows in your database. */Author Stevens=NewAuthor (); Stevens.setfirstname ("W."); Stevens.setlastname ("Stevens"); Authorpeer.doinsert (Stevens); /** Using The convenience methods to handle the foreign keys. */Book effective=NewBook (); Effective.settitle ("Effective Java"); EFFECTIVE.SETISBN ("0-618-12902-2"); Effective.setpublisher (Addison); Effective.setauthor (Bloch); Effective.save (); /** Inserting the Foreign-keys manually. */Book Tcpip=NewBook (); Tcpip.settitle ("TCP/IP Illustrated, Volume 1"); TCPIP.SETISBN ("0-201-63346-9"); Tcpip.setpublisherid (Addison.getpublisherid ()); Tcpip.setauthorid (Stevens.getauthorid ()); Tcpip.save (); /** Selecting All books from the database and printing the results to * stdout using our helper Method defined in Bookpeer (Doselectall). */System.out.println ("Full booklist:\n"); List<Book> Booklist =Bookpeer.doselectall (); Printbooklist (Booklist); /** Selecting specific objects. Just search for objects the match * This criteria (and the print to stdout). */System.out.println ("Booklist (Specific ISBN): \ n"); Criteria Crit=NewCriteria (); Crit.where (BOOKPEER.ISBN,"0-201-63346-9"); Booklist=Bookpeer.doselect (crit); Printbooklist (Booklist); /** Updating data. These lines would swap the authors of the and the books. The Booklist is printed to stdout to verify the results. */Effective.setauthor (Stevens); Effective.save (); Tcpip.setauthor (Bloch); Bookpeer.doupdate (TCPIP); System.out.println ("Booklist (authors swapped): \ n"); Booklist=Bookpeer.doselectall (); Printbooklist (Booklist); /** Deleting data. These lines would delete the data that matches the * specified criteria. */Crit=NewCriteria (); Crit.where (BOOKPEER.ISBN,"0-618-12902-2"); Bookpeer.dodelete (Crit); Crit=NewCriteria (); Crit.where (BOOKPEER.ISBN,"0-201-63346-9"); Crit.and (Bookpeer.title,"TCP/IP Illustrated, Volume 1"); Bookpeer.dodelete (Crit); /** Deleting data by passing data Objects instead of specifying * criteria. */Authorpeer.dodelete (Bloch); Authorpeer.dodelete (Stevens); Publisherpeer.dodelete (Addison); System.out.println ("Booklist (should be empty): \ n"); Booklist=Bookpeer.doselectall (); Printbooklist (Booklist); } Catch(Exception e) {e.printstacktrace (); } } /** Helper method to print a booklist. */ Private Static voidPrintbooklist (list<book>Booklist) { for(book book:booklist) {System.out.println (book); } }}
View CodeReference
Torque tutorial:https://db.apache.org/torque/torque-4.0/documentation/tutorial/orm/index.html
Apache Torque Getting Started learning