JDO : Annotations

One of the things that JDK 1.5 provides that can be of some use is annotations, and JDO provides its
own set. When selecting to use annotations please bear in mind the following :-

You must have the datanucleus-api-jdo jar available in your CLASSPATH.

You must have the jdo-api jar in your CLASSPATH since this provides the annotations

Annotations should really only be used for attributes of persistence that you won't be changing
at deployment. Things such as table and column names shouldn't really be specified using annotations
although it is permitted. Instead it would be better to put such information in an ORM MetaData file.

Annotations can be added in two places - for the class as a whole, or for a field in particular.

You can annotate fields or getters with field-level information. If you annotate fields then the
fields are processed for persistence. If you annotate the methods (getters) then the methods (properties)
are processed for persistence.

Annotations are prefixed by the @ symbol and can take properties (in brackets after the name,
comma-separated)

Annotations supported by DataNucleus are shown below.
The annotations/attributes coloured in pink are ORM and really should be placed in XML rather than directly
in the class using annotations.

@PersistenceAware

This annotation is used when you want to mark a class as being used in persistence but not being persistable.
That is "persistence-aware" in JDO2 terminology. It has no attributes.
Specified on the class.

@Index

Annotation used to define an index for the class as a whole typically being a composite index across
multiple columns or fields/properties.
Is equivalent to the <index> metadata element when specified under class.
Specified on the class.

@Unique

Annotation used to define a unique constraints for the class as a whole typically being a composite constraint across
multiple columns or fields/properties.
Is equivalent to the <unique> metadata element when specified under class.
Specified on the class.

Recursion depth for this field when fetching.
Only applicable when specified within @FetchGroup

1

loadFetchGroup

String

Name of a fetch group to activate when a load of this field is initiated (due to it
being currently unloaded). Not used for getObjectById, queries, extents etc.
Better to use @FetchGroup and define your groups

@Transactional

This annotation is a shortcut for @Persistent(persistenceModifier=PersistenceModifier.TRANSACTIONAL) meaning that the
field/property is not persisted yet managed. It has no attributes. Specified on the field/method.

@PrimaryKey

This annotation is a shortcut for @Persistent(primaryKey="true") meaning that the field/property is
part of the primary key for the class.
No attributes are needed when specified like this. Specified on the field/method.

@Column

Annotation used to define that the colum where a field is persisted.
Is equivalent to the <column> metadata element when specified under field.
Specified on the field/method (and within other annotations).

Attribute

Type

Description

Default

name

String

ORM : Name of the column

target

String

ORM : Column in the other class that this maps to

targetMember

String

ORM : Field/Property in the other class that this maps to

jdbcType

String

ORM : JDBC Type to use for persisting into this column

sqlType

String

ORM : SQL Type to use for persisting into this column

length

int

ORM : Max length of data to store in this column

scale

int

ORM : Max number of floating points of data to store in this column

allowsNull

String

ORM : Whether null is allowed to be persisted into this column

defaultValue

String

ORM : Default value to persist into this column. If you want the
default to be NULL, then put this as "#NULL"

insertValue

String

ORM : Value to insert into this column when it is an "unmapped" column. If you want the
inserted value to be NULL, then put this as "#NULL"