This sample demonstrates how to use XDoclet comment tags to create JDO metadata. The Main class is a persistent type with appropriate comment tags. The build.xml file invokes XDoclet to create a JDO metadata file from the commented source.
Example 1.46. Main.java
package samples.xdoclet;
import java.util.*;
/**
* <p>This is a simple example class using XDoclet. It is used to demonstrate
* automatic generation of the JDO metadata file based on the
* <code>jdo.*</code> XDoclet tags in the source code.</p>
*
* <p>The JDO tags above the class element apply to class-level metadata. The
* <code>jdo.persistence-capable</code> tag is required to denote a persistent
* class. The tag takes the same attribues as the <code>class</code> element
* in standard JDO metadata. You can include class-level vendor extensions
* with the <code>jdo.class-vendor-extension</code> tag.</p>
*
* @jdo.persistence-capable
* identity-type="application"
* objectid-class="Main$Id"
* @jdo.class-vendor-extension
* vendor-name="kodo"
* key="data-cache-timeout"
* value="0"
*/
public class Main
{
/**
* Field-level metadata is declared with the <code>jdo.field</code> tag.
* It takes the same attributes as the <code>field</code> element in
* standard JDO metadata.
*
* @jdo.field
* primary-key="true"
* @jdo.field-vendor-extension
* vendor-name="kodo"
* key="jdbc-auto-increment"
* value="true"
*/
private String pk1;
/**
* You are not required to place all attributes on a separate line as
* we have been doing above.
*
* @jdo.field primary-key="true"
* @jdo.field-vendor-extension vendor-name="kodo"
* key="jdbc-size" value="20"
*/
private String pk2;
/**
* @jdo.field
* null-value="exception"
* default-fetch-group="false"
*/
private String name;
private Main main;
/**
* @jdo.field
* default-fetch-group="true"
* collection-type="collection"
* element-type="Main"
* embedded-element="false"
* @jdo.field-vendor-extension
* vendor-name="kodo"
* key="inverse-owner"
* value="main"
*/
private Collection nodes = new ArrayList ();
/**
* @jdo.field
* collection-type="map"
* key-type="String"
* value-type="Integer"
*/
private Map cache = new HashMap ();
public String getPk1 ()
{
return this.pk1;
}
public void setPk1 (String pk1)
{
this.pk1 = pk1;
}
public String getPk2 ()
{
return this.pk2;
}
public void setPk2 (String pk2)
{
this.pk2 = pk2;
}
public String getName ()
{
return this.name;
}
public void setName (String name)
{
this.name = name;
}
public Main getMain ()
{
return this.main;
}
public void setMain (Main main)
{
this.main = main;
}
public Collection getNodes ()
{
return this.nodes;
}
public Map getCache ()
{
return this.cache;
}
/**
* Application identity class.
*/
public static class Id
{
private static final char DELIM = '/';
public String pk1;
public String pk2;
public Id ()
{
}
public Id (String serialized)
{
int idx = serialized.indexOf (DELIM);
pk1 = serialized.substring (0, idx);
pk2 = serialized.substring (idx + 1);
}
public boolean equals (Object other)
{
if (other == this)
return true;
if (!(other instanceof Id))
return false;
Id id = (Id) other;
return pk1.equals (id.pk1) && pk2.equals (id.pk2);
}
public int hashCode ()
{
return pk1.hashCode () + pk2.hashCode ();
}
public String toString ()
{
return pk1 + DELIM + pk2;
}
}
}