Chapter 4. PersistenceCapable
Prev | Part II. Java Data Objects | Next

## Chapter 4. PersistenceCapable

In JDO, all user-defined persistent classes implement the `javax.jdo.spi.PersistenceCapable` interface. This interface contains many complex methods that enable the JDO implementation to manage the persistent fields of class instances. Fortunately, you do not have to implement this interface yourself. In fact, writing a persistent class in JDO is usually no different than writing any other class. There are no special parent classes to extend from, field types to use, or methods to write. This is one important way in which JDO makes persistence completely transparent to you, the developer.

Example 4.1. PersistenceCapable Class

    package org.mag;

    /**
     * Example persistent class.  Notice that it looks exactly like any other
     * class.  JDO makes writing persistent classes completely transparent.
     */
    public class Magazine
    {
        private String    isbn;
        private String    title;
        private Set       articles = new HashSet ();
        private Date      copyright;
        private Company   publisher;

        private Magazine ()
        {
        }

        public Magazine (String title, String isbn)
        {
            this.title = title;
            this.isbn = isbn;
        }

        public void publish (Company publisher, Date copyright)
        {
            if (copyright == null)
                copyright = new Date ();

            this.publisher = publisher;
            publisher.addMagazine (this);
            this.copyright = copyright;
        }

        public void addArticle (Article article)
        {
            articles.add (article);
        }

        // rest of methods omitted
    }

## 4.1. JDO Enhancer

In order to shield you from the intricacies of the `PersistenceCapable` interface, most JDO implementations provide an enhancer. An enhancer is a tool that automatically adds code to your persistent classes after you have written them. Though some vendors may use source enhancers that modify your Java code, enhancers generally operate on `.class` files. They post-process the bytecode generated by your Java compiler, adding the necessary fields and methods to implement the `PersistenceCapable` interface. JDO's bytecode modification perfectly preserves the line numbers in stack traces and is compatible with Java debuggers, so enhancement does not affect debugging.

The diagram above illustrates the compilation of a persistent class. JDO implementations typically include an Ant enhancer task so that you can make enhancement an automatic part of your build process.

All JDO enhancers are required to be binary-compatible with each other. This means that the final enhanced class can be used not only by the JDO implementation whose enhancer created it, but by any other JDO implementation as well. The binary compatibility requirement ensures that you can package and ship persistent classes to other developers without worrying about what JDO vendor they use. It also means that you can switch JDO vendors without even recompiling your persistent classes.

Prev | Up | Next
Chapter 3. JDO Architecture | Home | 4.2. Persistence-Capable vs. Persistence-Aware