net.sourceforge.pbeans
Interface Persistent


Deprecated. Use a PersistentClass annotation instead.

public interface Persistent

JavaBeans tagged with this interface can be used with Store.

Getters and Setters

Classes that implement this interface define properties with getters and setters. For example, class User may define a property named userName as follows:
    public class User implements Persistent {
        private String uname;

        public String getUserName() {
            return this.uname;
        }

        public void setUserName(String u) {
            this.uname = u;
        }
    }
 

Persistent Properties

By default, properties of certain compile-time types are considered persistent. These types are PersistentID, String, java.util.Date, java.sql.Date, java.sql.Timestamp, primitive types, and boxed primitive types (such as Integer, Long, etc.) Additionally, properties with a compile-time type assignable to Persistent also persist by default.

Customization

You may define a class that accompanies the Persistent JavaBean in order to define which properties persist, how they are mapped into database fields, how those fields are indexed, etc. The accompanying class must implement StoreInfo or preferably extend AbstractStoreInfo, and have the same name as the JavaBean but suffixed with _StoreInfo.

Collections

The recommended technique for defining whole/part relationships is to define a "foreign key" property in the part. For example, suppose User can have zero or more instances of Account. We could make Account have a property named owner as follows:
    public class Account implements Persistent {
        private User owner;

        public User getOwner() {
            return this.owner;
        }

        public void setOwner(User u) {
            this.owner = u;
        }
    }
 
A more efficient alternative is to use a property of type PersistentID so the User instance is not forced to be loaded from secondary storage when we obtain an instance of Account.
    public class Account implements Persistent {
        private PersistentID ownerID;

        public PersistentID getOwnerID() {
            return this.ownerID;
        }

        public void setOwnerID(PersistentID pid) {
            this.ownerID = pid;
        }

        public User fetchOwner(Store store) {
            return (User) store.getObject(this.ownerID, User.class);
        }
    }
 
In order to add an account to a user, we simply set the ownerID property. And to obtain all accounts that belong to a user we call Store.selectParts.
    public class User implements Persistent {
        public void addAccount(Store store, Account account) {
            account.setOwnerID(store.getPersistentID(this));
        }

        public Iterator fetchAccounts(Store store) {
            return store.selectParts(store.getPersistentID(this), Account.class, "ownerID");
        }
    }
 

Note on Synchronization

If you need to synchronize getters and setters, do not invoke Store from synchronized sections of those methods because that could result in a deadlock.