官方网站:http://www.castor.org/index.html
所需Jar包一览:
castor-1.2.jar
castor-1.2-anttasks.jar
castor-1.2-codegen.jar
castor-1.2-ddlgen.jar
castor-1.2-jdo.jar
castor-1.2-xml.jar
castor-1.2-xml-schema.jar
jdbc-se2.0.jar
jta1.0.1.jar
xerces-J_1.4.0.jar
commons-logging-1.1.jar
mysql-connector-java-5.1.6-bin.jar
log4j-1.2.13.jar
castor.properties
- # Example properties indent the output, require validation
- # on input and turn debugging on.
- #
- org.exolab.castor.validation=false
- org.exolab.castor.indent=true
- org.exolab.castor.debug=true
- # True if database configuration should be initalization
- # when loading it (default: true).
- #
- #org.exolab.castor.jdo.DatabaseInitializeAtLoad=true
- #
- # TransactionManagerFactory implementations
- #
- org.castor.transactionmanager.Factories=/
- org.castor.transactionmanager.WebSphereTransactionManagerFactory,/
- org.castor.transactionmanager.WebSphere5TransactionManagerFactory,/
- org.castor.transactionmanager.WebSphere51TransactionManagerFactory,/
- org.castor.transactionmanager.LocalTransactionManagerFactory,/
- org.castor.transactionmanager.JNDIENCTransactionManagerFactory,/
- org.castor.transactionmanager.JOTMTransactionManagerFactory
- #
- # Shell the TransactionManager be initialized at registration or lazily when
- # requested for the first time? Defaults to false.
- #
- org.castor.transactionmanager.InitializeAtRegistration=false
工程布局:
jdo-conf.xml
- <?xml version="1.0" ?>
- <!DOCTYPE jdo-conf PUBLIC "-//EXOLAB/Castor JDO Configuration DTD Version 1.0//EN"
- "http://castor.org/jdo-conf.dtd">
- <jdo-conf>
- <database name="castordb" engine="mysql" >
- <driver url="jdbc:mysql://localhost:3306/castordb"
- class-name="com.mysql.jdbc.Driver">
- <param name="user" value="root" />
- <param name="password" value="root" />
- </driver>
- <mapping href="jdo/mapping.xml" />
- </database>
- <transaction-demarcation mode="local">
- <transaction-manager name="local" />
- </transaction-demarcation>
- </jdo-conf>
mapping.xml
- <?xml version="1.0" ?>
- <!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
- "http://castor.org/mapping.dtd">
- <mapping>
- <class name="myapp.ProductGroup" identity="id">
- <description>Product group</description>
- <map-to table="prod_group" xml="group" />
- <field name="id" type="integer">
- <sql name="id" type="integer" />
- <bind-xml node="attribute" />
- </field>
- <field name="name" type="string">
- <sql name="name" type="char" dirty="check" />
- <bind-xml node="text" />
- </field>
- </class>
- <class name="myapp.Product" identity="id">
- <description>Product definition</description>
- <map-to table="prod" xml="product" />
- <field name="id" type="integer">
- <sql name="id" type="integer" />
- <bind-xml name="id" node="attribute" />
- </field>
- <field name="name" type="string">
- <sql name="name" type="char" />
- <bind-xml name="name" node="element" />
- </field>
- <field name="price" type="float">
- <sql name="price" type="numeric" />
- <bind-xml name="price" node="element" />
- </field>
- <field name="group" type="myapp.ProductGroup">
- <sql name="group_id" />
- <bind-xml name="group" node="element" />
- </field>
- <field name="details" type="myapp.ProductDetail" required="true"
- collection="vector">
- <sql many-key="prod_id" />
- <bind-xml name="detail" node="element" />
- </field>
- <field name="categories" type="myapp.Category" required="true" collection="vector">
- <sql name="category_id" many-table="category_prod"
- many-key="prod_id" />
- <bind-xml name="category" node="element" />
- </field>
- </class>
- <class name="myapp.ProductDetail" identity="id" depends="myapp.Product">
- <description>Product detail</description>
- <map-to table="prod_detail" xml="detail" />
- <field name="id" type="integer">
- <sql name="id" type="integer" />
- <bind-xml node="attribute" />
- </field>
- <field name="product" type="myapp.Product">
- <sql name="prod_id" />
- <bind-xml name="product" node="element" reference="true" />
- </field>
- <field name="name" type="string">
- <sql name="name" type="char" />
- <bind-xml node="text" />
- </field>
- </class>
- <class name="myapp.Computer" extends="myapp.Product" identity="id">
- <description>
- Computer definition, extends generic product
- </description>
- <map-to table="computer" xml="computer" />
- <field name="id" type="integer">
- <sql name="id" type="integer" />
- <bind-xml name="id" node="attribute" />
- </field>
- <field name="cpu" type="string">
- <sql name="cpu" type="char" />
- <bind-xml node="element" />
- </field>
- </class>
- <class name="myapp.Category" identity="id">
- <description>
- A product category, any number of products can belong to the
- same category, a product can belong to any number of
- categories
- </description>
- <map-to table="category" xml="category" />
- <field name="id" type="integer">
- <sql name="id" type="integer" />
- <bind-xml />
- </field>
- <field name="name" type="string">
- <sql name="name" type="char" />
- <bind-xml node="element" />
- </field>
- <field name="products" type="myapp.Product" required="true"
- collection="vector">
- <sql name="prod_id" many-table="category_prod"
- many-key="category_id" />
- <bind-xml name="product" node="element" />
- </field>
- </class>
- </mapping>
Category.java
- package myapp;
- import java.util.Vector;
- public class Category {
- private int _id;
- private Vector<Product> _products = new Vector<Product>();
- private String _name;
- public int getId(){
- return _id;
- }
- public void setId( int id ) {
- _id = id;
- }
- public String getName() {
- return _name;
- }
- public void setName( String name ){
- _name = name;
- }
- public Vector<Product> getProducts() {
- return _products;
- }
- public void addProduct( Product product ) {
- if ( ! _products.contains( product ) ) {
- System.out.println( "Adding product " + product + " to category " + this );
- _products.addElement( product );
- product.addCategories( this );
- }
- }
- public String toString() {
- return "<id: " + _id + " name: " + _name + ">";
- }
- }
Computer.java
- package myapp;
- import org.exolab.castor.jdo.TimeStampable;
- public class Computer extends Product implements TimeStampable {
- private String _cpu;
- private long _timeStamp;
- public String getCpu() {
- return _cpu;
- }
- public void setCpu(String cpu) {
- _cpu = cpu;
- }
- public void jdoSetTimeStamp(long timeStamp) {
- _timeStamp = timeStamp;
- }
- public long jdoGetTimeStamp() {
- return _timeStamp;
- }
- public String toString() {
- return "<id: " + getId() + " name: " + getName() + " price: "
- + getPrice() + " cpu: " + _cpu + ">";
- }
- }
Product.java
- package myapp;
- import java.util.Vector;
- import org.exolab.castor.jdo.Database;
- import org.exolab.castor.jdo.Persistent;
- import org.exolab.castor.jdo.TimeStampable;
- import org.exolab.castor.mapping.AccessMode;
- public class Product implements Persistent, TimeStampable {
- private int _id;
- private String _name;
- private float _price;
- private ProductGroup _group;
- private long _timeStamp;
- private Vector<ProductDetail> _details = new Vector<ProductDetail>();
- private Vector<Category> _categories = new Vector<Category>();
- public int getId() {
- return _id;
- }
- public void setId(final int id) {
- _id = id;
- }
- public String getName() {
- return _name;
- }
- public void setName(final String name) {
- _name = name;
- }
- public float getPrice() {
- return _price;
- }
- public void setPrice(final float price) {
- _price = price;
- }
- public ProductGroup getGroup() {
- return _group;
- }
- public void setGroup(final ProductGroup group) {
- _group = group;
- }
- public ProductDetail createDetail() {
- return new ProductDetail();
- }
- public Vector<ProductDetail> getDetails() {
- return _details;
- }
- public void addDetail(final ProductDetail detail) {
- _details.add(detail);
- detail.setProduct(this);
- }
- public Vector<Category> getCategories() {
- return _categories;
- }
- public void addCategories(final Category category) {
- if (!_categories.contains(category)) {
- _categories.addElement(category);
- category.addProduct(this);
- }
- }
- public void jdoPersistent(final Database db) {
- }
- public void jdoTransient() {
- }
- public Class<Computer> jdoLoad(final AccessMode accessMode) {
- if (_name.indexOf("PC") >= 0) {
- return Computer.class;
- }
- return null;
- }
- public void jdoBeforeCreate(final Database db) {
- }
- public void jdoAfterCreate() {
- }
- public void jdoStore(final boolean modified) {
- }
- public void jdoBeforeRemove() {
- }
- public void jdoAfterRemove() {
- }
- public void jdoUpdate() {
- }
- public void jdoSetTimeStamp(final long timeStamp) {
- _timeStamp = timeStamp;
- }
- public long jdoGetTimeStamp() {
- return _timeStamp;
- }
- public String toString() {
- return "<id: " + _id + " name: " + _name + " price: " + _price + ">";
- }
- }
ProductDetail.java
- package myapp;
- public class ProductDetail {
- private int _id;
- private Product _product;
- private String _name;
- public int getId() {
- return _id;
- }
- public void setId(int id) {
- _id = id;
- }
- public String getName() {
- return _name;
- }
- public void setName(String name) {
- _name = name;
- }
- public Product getProduct() {
- return _product;
- }
- public void setProduct(Product product) {
- _product = product;
- }
- public String toString() {
- return "<id: " + _id + " name: " + _name + ">";
- }
- }
- package myapp;
- public class ProductGroup {
- private int _id;
- private String _name;
- public int getId() {
- return _id;
- }
- public void setId(int id) {
- _id = id;
- }
- public String getName() {
- return _name;
- }
- public void setName(String name) {
- _name = name;
- }
- public String toString() {
- return "<id: " + _id + " name: " + _name + ">";
- }
- }
测试代码:
CastorDemo.java
- package jdo;
- import java.sql.Connection;
- import java.sql.Statement;
- import myapp.Category;
- import myapp.Computer;
- import myapp.Product;
- import myapp.ProductDetail;
- import myapp.ProductGroup;
- import org.exolab.castor.jdo.Database;
- import org.exolab.castor.jdo.JDOManager;
- import org.exolab.castor.jdo.OQLQuery;
- import org.exolab.castor.jdo.QueryResults;
- import org.exolab.castor.mapping.Mapping;
- public class CastorDemo {
- private static final String DATABASE_NAME = "castordb";
- public static final String JDO_CONFIG_FILE = "jdo-conf.xml";
- public static final String MAPPING_FILE = "mapping.xml";
- public static void main(String[] args) throws Exception {
- Mapping mapping = new Mapping(CastorDemo.class.getClassLoader());
- mapping.loadMapping(CastorDemo.class.getResource(MAPPING_FILE));
- String jdoConf = CastorDemo.class.getResource(JDO_CONFIG_FILE).toString();
- JDOManager.loadConfiguration(jdoConf);
- JDOManager manager = JDOManager.createInstance(DATABASE_NAME);
- Database db = manager.getDatabase();
- createTable(manager);
- db.begin();
- ProductGroup group = new ProductGroup();
- group.setId( 3 );
- group.setName( "a group" );
- db.create( group );
- Product product = new Product();
- product.setId( 4 );
- product.setName( "product4" );
- product.setPrice( 200 );
- product.setGroup( group );
- ProductDetail detail = new ProductDetail();
- detail.setId( 1 );
- detail.setName( "keyboard" );
- product.addDetail( detail );
- detail = new ProductDetail();
- detail.setId( 2 );
- detail.setName( "mouse" );
- product.addDetail( detail );
- detail = new ProductDetail();
- detail.setId( 3 );
- detail.setName( "monitor" );
- product.addDetail( detail );
- db.create( product );
- Category category = new Category();
- category.setId( 1 );
- category.setName( "category" + category.getId() );
- category.addProduct( product );
- db.create( category );
- Computer computer = new Computer();
- computer.setId( 7 );
- computer.setCpu( "Pentium" );
- computer.setName( "MyPC" );
- computer.setPrice( 400 );
- computer.setGroup( group );
- detail = new ProductDetail();
- detail.setId( 4 );
- detail.setName( "network card" );
- computer.addDetail( detail );
- detail = new ProductDetail();
- detail.setId( 5 );
- detail.setName( "scsi card" );
- computer.addDetail( detail );
- db.create(computer);
- db.commit();
- //======================================================
- db.begin();
- OQLQuery productOql = db.getOQLQuery( "SELECT p FROM myapp.Product p WHERE p.id > $1" );
- productOql.bind( 1 );
- QueryResults results = productOql.execute();
- while(results.hasMore()) {
- product = ( Product ) results.next();
- System.out.println(product.getName());
- //db.remove( product );
- }
- db.commit();
- //======================================================
- db.begin();
- product = ( Product ) db.load( Product.class, 7 );
- System.out.println(product.getName() + " " + product.getPrice());
- db.commit();
- db.begin();
- product.setPrice(700);
- db.update(product);
- db.commit();
- //======================================================
- // Marshaller marshaller = new Marshaller( new PrintWriter(System.out));
- // marshaller.setMapping( mapping );
- // marshaller.marshal(product);
- //
- // db.begin();
- // OQLQuery computerOql = db.getOQLQuery( "SELECT c FROM myapp.Computer c WHERE c.id >= $1" );
- // computerOql.bind( 7 );
- // results = computerOql.execute();
- //
- // while( results.hasMore() )
- // marshaller.marshal( results.next() );
- // db.commit();
- db.close();
- // MappingRoot mappingRoot = mapping.getRoot();
- // ClassMapping classMap;
- // FieldMapping fieldMap;
- // FieldMapping[] fieldMaps;
- // int mappingCount = mappingRoot.getClassMappingCount();
- // for ( int i = 0; i < mappingCount; ++i ){
- // classMap = mappingRoot.getClassMapping( i );
- // fieldMaps = classMap.getClassChoice().getFieldMapping();
- // for ( int j = 0; j < fieldMaps.length; ++j ){
- // fieldMap = fieldMaps[j];
- // System.out.println(" Field name: " + fieldMap.getName());
- // System.out.println(" Field type: " + fieldMap.getType());
- // }
- // }
- }
- public static void createTable(JDOManager manager) throws Exception {
- Database db = manager.getDatabase();
- db.begin();
- Connection connection = db.getJdbcConnection();
- Statement statement = connection.createStatement();
- statement.executeUpdate("create table prod (id int not null, name varchar(200) not null, price numeric(18,2) not null, group_id int not null)");
- statement.executeUpdate("create table prod_group (id int not null, name varchar(200) not null)");
- statement.executeUpdate("create table prod_detail (id int not null, prod_id int not null, name varchar(200) not null)");
- statement.executeUpdate("create table computer (id int not null, cpu varchar(200) not null)");
- statement.executeUpdate("create table category (id int not null, name varchar(200) not null)");
- statement.executeUpdate("create table category_prod (prod_id int not null, category_id int not null)");
- db.commit();
- db.close();
- }
- }
Castor介绍资料:
http://www.ibm.com/developerworks/cn/xml/x-xjavacastor1/
http://www.ibm.com/developerworks/cn/xml/x-xjavacastor2/