/** * A sample class shows the Value Object construction */ public class AuctionValueObject implements java.io.Serializable {
public String seller; public String description; public double startPrice; public String summary; public AuctionValueObject() {}
// constructor accepts all values public AuctionValueObject(String seller, String description, double startPrice, String summary) { init(seller, description, startPrice, summary); }
// new constructor based on previous data public AuctionValueObject(AuctionValueObject avo) { init(avo.seller, avo.description, avo.startPrice, avo.summary); }
// method to set the values. public void init(String seller, String description, double startPrice, String summary) { this.seller = seller; this.description = description; this.startPrice = startPrice; this.summary = summary; }
// method creates a new AuctionValueObject public AuctionValueObject getData() { return new AuctionValueObject(this); } }
Og her er bønnen, som nedarver fra AuctionValueObject:
package valueobject;
import java.rmi.*; import javax.ejb.*;
public class AuctionItemBean extends AuctionValueObject implements EntityBean { EntityContext entityContext; public String ejbCreate() throws CreateException, RemoteException { // @todo: Implement this method return null; } public void ejbPostCreate() throws CreateException, RemoteException { } public void ejbLoad() throws RemoteException { } public void ejbStore() throws RemoteException { } public void ejbRemove() throws RemoveException, RemoteException { } public void ejbActivate() throws RemoteException { } public void ejbPassivate() throws RemoteException { } public void setEntityContext(EntityContext entityContext) throws RemoteException { this.entityContext = entityContext; } public void unsetEntityContext() throws RemoteException { entityContext = null; } public String ejbFindByPrimaryKey(String primKey) throws ObjectNotFoundException { //@todo: Implement this method return null; } }
Det er jo så meningen at en klient skal kalde getData() på AuctionValueObject, som så indeholde al data fra bønnen. Herefter skal AuctionValueObject så retunerer et object af sig selv.
Men hvordan får AuctionValueObject fat i data fra bønnen? Og hvordan skal klienten se ud?
Jeg har lavet lidt om i koden på bønnen og Value-objektet: Min spørgsmål er stadig hvordan Value-objektet får data fra bønnen, blot ved at kalde getData() på Value-objektet?
/** * A sample class shows the Value Object construction */ public class AuctionValueObject implements java.io.Serializable {
public int id; public String password; public String username; public String displayname; public Integer role; public Timestamp created; public String realname; public Integer active;
public String seller; public String description; public double startPrice; public String summary; public AuctionValueObject() {}
// new constructor based on previous data public AuctionValueObject(AuctionValueObject avo) { init(avo.id, avo.password, avo.username, avo.role, avo.created, avo.realname, avo.active); }
// method to set the values. public void init (int id, String password, String username, Integer role, Timestamp created, String realname, Integer active)
public String getStr1() { return str1; } public void setStr1(String str1) { this.str1 = str1; } }
Så er din klient lige efter bogen: UserHome h = ..... retrieve home ... User u = h.findByPrimaryKey(new Integer(12)); UserVO = u.getData(); // Off-load VO from bean u = null; // Drop the bean
// Reverse, when setting data again.
Klienten er typisk en session bean, der fungerer som facade for entity beans.
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.