5.3. Class Element
Prev | Chapter 5. Metadata | Next

## 5.3. Class Element

`package` elements contain one or more `class` elements, surrounded by zero or more `extension` elements. Every persistent class in the package named by each `package` element must be represented by a `class` element. Before we explore this element in detail, a brief note on how JDO resolves class names is in order.

Several metadata attributes require you to specify class names. The names you give should follow these guidelines:

- If the class is in the package named by the current `package` element, you can give just the class name, without specifying the package. For example, if the current `package` name is `org.mag` and the class is `org.mag.Magazine`, then you can simply write `Magazine` for the class name.

- Similarly, if the class is in `java.lang`, `java.util`, or `java.math` packages, you do not need to specify the package in the class name.

- Otherwise, use the full class name, including package name.

- If the class is an inner class, then write it as `<parent-class>$<inner-class>` For example, `Subscription$LineItem`.

We now turn our attention back to the `class` element. This element has the following attributes:

- `name`: The unqualified name of the class. This attribute is required.

- `persistence-modifier`: This attribute defaults to `persistence-capable`, denoting a persistent class. You can, however, also list persistence-aware classes in metadata by setting this attribute to `persistence-aware` (see Section 4.2, “Persistence-Capable vs. Persistence-Aware” for a discussion of persistence-aware classes). In this case, the class metadata should have no other content. Listing persistence-aware classes in metadata is optional.

- `identity-type`: Gives the JDO identity type used by the class. Legal values are `application` for application identity, `datastore` for datastore identity, and `none`. This attribute defaults to a value of `application` if you declare any primary key fields or an `objectid-class`, and `datastore` otherwise.

- `objectid-class`: For application identity, the name of the JDO identity class used by this persistent class. If this is a persistent subclass that uses the same objectid-class as its superclass, you do not have to specify this attribute. Do not specify this attribute if you use single field identity (see Section 4.5.3, “Single Field Identity”).

- `requires-extent`: Set this attribute to `false` if you will never need to query for persistent instances of this class (i.e., if all objects of the class can be obtained through JDO identity lookups or through relations with other objects). Defaults to `true`.

- `embedded-only`: Set this attribute to `true` to indicate that this class will only be used in embedded relations, and does not require any independent representation in the datastore. When `embedded-only` is `true`, `requires-extent` defaults to `false`.

- `detachable`: Whether instances of this class can be detached from a `PersistenceManager` and later re-attached to apply offline changes. The default is `false`. See Section 8.7, “Detach and Attach Functionality” for details on detachment.

Example 5.2. Metadata Class Listings

    <?xml version="1.0"?>
    <jdo>
        <package name="org.mag">
            <!-- application identity -->
            <class name="Magazine" objectid-class="Magazine$MagazineId">
                ...
            </class>
            <!-- single field identity -->
            <class name="Article" identity-type="application">
                ...
            </class>
        </package>
        <package name="org.mag.pub">
            <!-- default datastore identity -->
            <class name="Company">
                ...
            </class>
            <class name="Author">
                ...
            </class>
            <class name="Address" embedded-only="true">
                ...
            </class>
        </package>
        <package name="org.mag.subscribe">
            <!-- single field identity -->
            <class name="Contract" identity-type="application">
                ...
            </class>
            <class name="Subscription">
                ...
            </class>
            <class name="LifetimeSubscription">
                ...
            </class>
            <class name="TrialSubscription">
                ...
            </class>
            <!-- static inner class -->
            <class name="Subscription$LineItem">
                ...
            </class>
        </package>
    </jdo>

Prev | Up | Next
5.2. JDO, Package, and Extension Elements | Home | 5.4. Field Element