Java Persistent Objects JDO (JPOX) relies on the use of JDO Meta-Data for the classes to be persisted. The Meta-Data file(s) define how a class is persisted, from the basic information like which fields are persisted, down to whether the user wants to specify what column names are used in the database etc. Generating JDO Meta-Data files can be time consuming and laborios. Additionally if you swap JDO providers you will need to update your Meta-Data because there are a substantial number of vendor specific extensions available. One way around the need to manually generate these files is to use XDoclet (JDODoclet). This is available from the XDoclet project. The process of using XDoclet is straightforward. You add Javadoc tags to the various attributes in your class while you are writing it, and your run XDoclet over your classes which generates the Meta-Data files for you.
To guide you through the tagging process, there is an example class below. It contains tags in the Javadoc headers for the class and for the attributes (fields). It should be self explanatory, when read in conjunction with the Meta-Data Guide.
package mydomain;
import ...;
/**
* This is a sample class. In reality it represents a Supplier to a shop.
*
* @author Andy Jefferson
*
* @jdo.persistence-capable identity-type="datastore"
* @jdo.class-vendor-extension vendor-name="jpox"
* key="table-name" value="MY_SUPPLIER"
**/
public class Supplier
{
/** Name of the Supplier
*
* @jdo.field persistence-modifier="persistent"
* @jdo.field-vendor-extension vendor-name="jpox"
* key="length" value="max 100"
**/
protected String name=null;
/** Products for the supplier
*
* @jdo.field persistence-modifier="persistent"
* collection-type="collection"
* element-type="net.ajsoft.WebShop.Inventory.Product"
* @jdo.collection-vendor-extension vendor-name="jpox"
* key="owner-field" value="supplier"
**/
protected Collection products=new HashSet();
/** Default constructor. */
protected Supplier()
{
}
...
}
So as you see, the @jdo tags represent XML elements in the Meta-Data file. When the above class is passed through XDoclet, you will get a Meta-Data file containing the following
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jdo PUBLIC
"-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 1.0//EN"
"http://java.sun.com/dtd/jdo_1_0.dtd">
<jdo>
<package name="net.ajsoft.WebShop.Inventory">
<class name="Supplier" identity-type="datastore">
<extension vendor-name="jpox" key="table-name" value="MY_SUPPLIER">
</extension>
<field name="name" persistence-modifier="persistent">
<extension vendor-name="jpox" key="length" value="max 100">
</extension>
</field>
<field name="products" persistence-modifier="persistent">
<collection element-type="net.ajsoft.WebShop.Inventory.Product">
<extension vendor-name="jpox" key="owner-field" value="supplier">
</extension>
</collection>
</field>
</class>
</package>
</jdo>
So, as you see, the class-level "datastore" tag is placed within the class Meta-Data XML tag, and the vendor-extension "table-name" for the class is placed at class level. Similarly, the field-level tags are placed within the field Meta-Data XML tags. There are obviously many more tags that can be added, using the information in the Meta-Data Guide for JPOX, but this should be enough to give you an idea how XDoclet can aid you in deploying JDO solutions to JPOX. In terms of XDoclet, you can find more extensive documentation on the XDoclet web site. Please be aware that the XDoclet documentation on that website is not necessarily correct - in the documentation for XDoclet 1.2 there are tags @sql.field, @sql.table listed. These tags are not used by XDoclet and you should use @jdo.class-vendor-extension, @jdo.field-vendor-extension, @jdo.collection-vendor-extension instead even though these are not even mentioned on their website. |