|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
PersistentClass
annotation instead.
public interface Persistent
JavaBeans
tagged with this interface can be used
with Store
.
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
also persist by default.
StoreInfo
or preferably extend
AbstractStoreInfo
, and
have the same name as the JavaBean but suffixed with
_StoreInfo
.
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");
}
}
Store
from synchronized
sections of those methods because that could result
in a deadlock.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |