Castor JDO Mapping
Documentation Author(s): Bruce Snyder Werner Guttmann
News Introduction The Mapping File The <mapping> element The <class> element The <map-to> element The <field> element The <sql> element
News
Release 1.0 M3:
|
- | Added collection type 'iterator'. |
- | Added collection type 'enumerate'. |
- | Added additional syntax for specifying the identity of a class. |
Introduction
The Castor mapping file also provides a mechanism for binding a Java
object model to a relational database model. This is usually referred to
as object-to-relational mapping (O/R mapping). O/R mapping bridges the gap
between an object model and a relational model.
The mapping file provides a description of the Java object model to
Castor JDO. Via Castor XML, Castor JDO uses the information in the mapping
file to map Java objects to relational database tables. The
following is a high-level example of a mapping file:
<mapping>
<class ... >
<map-to ... />
<field ... >
<sql ... />
</field>
...
</class>
</mapping> |
|
Each Java object is represented by a <class> element composed of attributes,
a <map-to> element and <field> elements. The <map-to> element
contains a reference to the relational table to which the Java object maps.
Each <field> element represents either a public class variable or the
variable's accessor/mutator methods (depending upon the mapping info). Each
<field> element is composed of attributes and one <sql> element.
The <sql> element represents the field in the relational table to which
the <field> element maps.
It is possible to use the mapping file and Castor's default behavior in
conjunction. When Castor handles an object but is unable to locate
information about it in the mapping file, it will rely upon its default
behavior. Castor makes use of the Java programming language
Reflection API to introspect the Java objects to determine the methods
to use. This is the reason some attributes are not required in the mapping
file.
The Mapping File
The <mapping> element
<!ELEMENT mapping ( description?, include*, class*, key-generator* )> |
|
The <mapping> element is the root element of a mapping file. It contains:
|
- | an optional description |
- | zero or more <include> which facilitates reusing mapping files |
- | zero or more <class> descriptions: one for each class we intend to give
mapping information |
- | zero or more <key-generator>: not used for XML mapping |
A mapping file look like this:
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object Mapping DTD Version 1.0//EN"
"http://castor.org/mapping.dtd">
<mapping>
<description>Description of the mapping</description>
<include href="other_mapping_file.xml"/>
<class name="A">
...
</class>
<class name="B">
...
</class>
</mapping> |
|
The <class> element
<!ELEMENT class ( description?, cache-type?, map-to?, field+ )>
<!ATTLIST class
name ID #REQUIRED
extends IDREF #IMPLIED
depends IDREF #IMPLIED
auto-complete ( true |false ) "false"
identity CDATA #IMPLIED
access ( read-only | shared | exclusive | db-locked ) "shared"
key-generator IDREF #IMPLIED > |
|
The <class> element contains all the information used to map a Java object
to a relational database. The content of <class> is mainly used to describe
the fields that will be mapped.
Description of the attributes:
|
- | name: The fully qualified package name of the Java object to map to. |
- | extends: Should be used only if this Java object extends another Java
object for which mapping information is provided. It should not be used if the
extended Java object is not referenced in the mapping file. |
- | depends: For more information on this field, please see
Dependent and related relationships. |
- | identity: For more information on this field, please see
Design -> Persistence. |
- | access: For more information on this field, please see
Locking Modes. |
- | key-generator: For more information on this field, please see
KeyGen. |
Description of the elements:
|
- | <description>: An optional description. |
- | <cache-type>: For more information on this field please see
Bounded Dirty Checking
and Caching. |
- | <map-to>: Used to tell Castor the name of the relational
table to which to map. |
- | <field>: Zero or more <field> elements are used to describe properties
of each Java object. |
The <map-to> element
<!ELEMENT map-to EMPTY>
<!ATTLIST map-to
table NMTOKEN #IMPLIED
xml NMTOKEN #IMPLIED
ns-uri NMTOKEN #IMPLIED
ns-prefix NMTOKEN #IMPLIED
ldap-dn NMTOKEN #IMPLIED
ldap-oc NMTOKEN #IMPLIED> |
|
<map-to> is used to specify the name of the item that should be associated
with the given Java object. The <map-to> element is only used for the root
Java object.
Description of the attributes:
|
- | table: The name of the relational database table to which the
Java object is associated. |
The <field> element
<!ELEMENT field ( description?, sql?, xml?, ldap? )>
<!ATTLIST field
name NMTOKEN #REQUIRED
type NMTOKEN #IMPLIED
required ( true | false ) "false"
direct ( true | false ) "false"
lazy ( true | false ) "false"
transient ( true | false ) "false"
identity ( true | false ) "false"
get-method NMTOKEN #IMPLIED
set-method NMTOKEN #IMPLIED
create-method NMTOKEN #IMPLIED
collection ( array | enumerate | collection | set |
arraylist | vector | map | hashtable | sortedset | iterator ) #IMPLIED
comparator NMTOKEN #IMPLIED> |
|
The <field> element is used to describe a property of a Java object. It provides:
|
- | the identity ('name') of the property |
- | the type of the property (inferred from 'type' and 'collection') |
- | the access method of the property (inferred from 'direct', 'get-method', 'set-method') |
From this information, Castor is able to access a given property in the Java
object.
In order to determine the signature that Castor expects, there are two easy
rules to apply.
1. Determine <type>.
If there is no 'collection' attribute, the object type is the value
of the 'type' attribute. The value of the type attribute can be a fully qualified Java
object like 'java.lang.String' or one of the allowed aliases:
short name |
Primitive type? |
Java Class |
big-decimal | N | java.math.BigDecimal |
big-integer | Y | java.math.BigInteger |
boolean | Y | java.lang.Boolean.TYPE |
byte | Y | java.lang.Byte.TYPE |
bytes | N | byte[] |
char | Y | java.lang.Character.TYPE |
chars | N | char[] |
clob | N | java.sql.Clob |
date | N | java.util.Date |
double | Y | java.lang.Double.TYPE |
float | Y | java.lang.Float.TYPE |
int | Y | java.lang.Integer.TYPE |
integer | Y | java.lang.Integer |
locale | N | java.util.Locale |
long | Y | java.lang.Long.TYPE |
other | N | java.lang.Object |
serializable | Y | java.io.Serializable |
short | Y | java.lang.Short.TYPE |
sqldate | Y | java.sql.Date |
sqltime | Y | java.sql.Date |
string | N | java.lang.String |
strings | N | String[] |
stream | N | java.io.InputStream |
timestamp | N | java.sql.Timestamp |
|
Castor will try to cast the data in the mapping file to the proper Java
type.
If there is a collection attribute, the items in the following table can be
used:
name |
type |
default implementation |
added in release |
array | <type_attribute>[] | <type_attribute>[] | |
enumerate | java.util.Enumeration | - | 1.0 M3 |
collection | java.util.Collection | java.util.ArrayList | |
set | java.util.Set | java.util.HashSet | |
arraylist | java.util.ArrayList | java.util.ArrayList | |
vector | java.util.Vector | java.util.Vector | |
map | java.util.Map | java.util.HashMap | |
hashtable | java.util.Hashtable | java.util.Hashtable | |
sortedset | java.util.SortedSet | java.util.TreeSet | 1.0 M2 |
iterator | java.util.Iterator | n/a | 1.0 M3 |
|
The type of the object inside the collection is the 'type' attribute. The 'default
implementation' is the type used if the object holding the collection is found
to be null and needs to be instantiated.
For hashtable and map, Castor will add an object using the put(object, object)
method - the object is both the key and the value. This will be improved in the future.
It is necessary to use a collection when the content model of the element expects more
than one element of the specified type. This is how the 'to-many' portion of a relationship
is described.
| It is not possible to use a collection of type 'iterator' or 'enumerate'
with lazy loading enabled. |
|
2. Determine the signature of the method
If 'direct' is set to true, Castor expects to find a public Java
object variable with the given signature:
If 'direct' is set to false or omitted, Castor will access the property
though accessor methods. Castor determines the signature of the accessors and mutators
as follows: If the 'get-method' or 'set-method' attributes are supplied, it will
try to find a function with the following signature:
public <type> <get-method>(); |
|
or
public void <set-method>(<type> value); |
|
If 'get-method' or 'set-method' attributes are not provided, Castor will try to
find the following functions:
public <type> is<capitalized-name>(); |
|
or
public <type> get<capitalized-name>(); |
|
the former for boolean types, the latter for all other types (or if the
'is<capitalized-name>()' method is not defined for a boolean type), and
a standard set method of
public void set<capitalized-name>(<type> value); |
|
If there are more than one set<capitalized-name> method it first tries to find the one
that exactly matches <type>. If no such method is available and <type> is a java
primitive type it tries to find a method with the corresponing java object type.
<capitalized-name> means that Castor uses the <name> attribute by changing its first
letter to uppercase without modifying the other letters.
The content of the <field> element will contain the information about how to map
this field to the relational table.
Description of the attributes:
|
- | name: If 'direct' access is used, 'name' should be the name of a public
variable in the object we are mapping (the field must be public, not
static and not transient). If no direct access and no 'get-/set-method'
is specified, this name will be used to infer the name of the accessor and
mutator methods. |
- | type: The Java type of the field. This is used to access the
field. Castor will use this information to cast the data type(e.g. string
into integer). It is also used to define the signature of the accessor and
mutator methods. If a collection is specified, this is used to specify the
type of the object inside the collection. See description above for more
details. |
- | required: If true, the field is not optional. |
- | transient: If true, the field will be ignored during persistence (and
XML un-/marshalling). If you want this field to be ignored during any
persistence-related operations only, please use the 'transient' attribute at the
<sql> level. |
- | identity: If true, the field is part of the class identity. Please use this
as an alternative way of specifying the identity of an object. |
- | direct: If true, Castor expects a public variable in the object
and will modify it directly. |
- | collection: If a parent object expects more than one occurrence of
one of its fields, it is necessary to specify which collection type Castor will
use to handle them. The type specified is used to define the type of the
content inside the collection. |
- | comparator: If the collection type equals 'sortedset', it is possible
to specify a java.util.Comparator instance that will be used with
the java.util.SortedSet (implementation) to specify a custom
sort order. Please use this attribute to specify the class name of the
Comparator instance to be used. Alternatively, it is possible to not
specify a Comparator instance and have the Java objects stored in the
SortedSet implement java.util.Comparable to specify the
sort order. |
- | get-method: An optional name of the accessor method Castor should
use. If this attribute is not set, Castor will try to guess the name with the
algorithm described above. |
- | set-method: An optional name of the mutator method Castor should
use. If this attribute is not set, Castor will try to guess the name with the
algorithm described above. |
- | create-method: Factory method for instantiation of the object. |
The <sql> element
<!ELEMENT sql EMPTY>
<!ATTLIST sql
name NMTOKENS #IMPLIED
type NMTOKENS #IMPLIED
many-key NMTOKENS #IMPLIED
many-table NMTOKEN #IMPLIED
transient ( true | false ) "false"
read-only ( true | false ) "false"
dirty ( check | ignore ) "check">
cascading ( create | delete | update | all | none ) "none" |
|
The <sql> element is used to denote information about the
database column to which a Java object is mapped. It should be declared
for all <field> elements. Each <field> element contains one <sql>
element. The <sql> element correlates directly to the <map-to>
element for the containing <class> element. The <sql> elements
contains the following attributes:
|
- | name: The name of the column in the database table. |
- | type: The JDBC type of the column. It is inferred from the object
when the type of this field is a persistent Java class that is defined
elsewhere in the mapping. The complete list of automatic type conversions, and
which values require manual mapping (e.g, java.util.Date) is listed in the
SQL Type Conversion section of the
Type Support document. |
- | read-only: If true, the column in the relational database
table will only be read, not updated or deleted. |
- | transient (as of 0.9.9): If true, the field will be ignored during persistence
only. If you want this field to be ignored during XML un-/marshalling as well,
please use the 'transient' attribute at the <field> level. |
- | dirty: If the value is 'ignore', the field will not be checked
against the database for modification. |
- | cascading: If the field is a relation, this attribute specifies which operations to cascade.
Possible values are: 'all', none', 'create', 'update' or 'delete'; when not specifying
'none' or 'all', it is possibel to specify more than one value, using whitespace
as a delimiter (e.g. 'create update'). For further information see
HOW-TO on using
cascading operation. |
There are two more attributes used only with 'to-many' relations.
|
- | many-key: Specifies the name of the column that holds the
foreign key to this object. That column is in the database table
that stores objects of the Java type of this field. |
- | many-table: Specifies the name of the bridge table that contains
the primary keys of the object on each side of the relationship. This is only
used for many-to-many relationships. |
|