V
VRGL
Unregistered / Unconfirmed
GUEST, unregistred user!
Design Pattern II
To avoid the disadvantages associated with Design Pattern I, we introduce the
concept of a value object that encapsulates all the data values associated with an
Entity Bean. The value object, in FoliQuest? terminology, has a “Struct”
appended to it because of their similarity to CORBA Structs.
Value Object code snippet for Company
public class CompanyStruct implements
java.io.Serializable {
public Integer comId;
//Primary Key
public String comName;
public String comDescription;
public java.sql.Timestamp mutationDate;
}
Value Object code snippet for Employee
public class EmployeeStruct implements
java.io.Serializable {
public Integer empId;
//Primary Key
public Integer comId;
//Foreign Key
public String empFirstName;
public String empLastName;
public java.sql.Timestamp mutationDate;
}
Now, the Company and Employee Entity Bean can accept the above Struct as a
parameter for the ejbCreate(). Since the Struct also encapsulates all the
data associated with an Entity Bean, only one getData() and a setData()
method is required to get and set all the fields in the Entity Bean.
Code snippet for an Entity Bean’s create()
public Integer ejbCreate(CompanyStruct struct) throws
CreateException {
this.comId = struct.comId;
this.comName = struct.comName;
this.comDescription = struct.comDescription;
this.mutationDate = struct.mutationDate;
return null;
}
Code snippet for an Entity Bean’s getData()
public CompanyStruct getData() {
CompanyStruct result = new CompanyStruct();
result.comId = this.comId;
result.comName = this.comName;
result.comDescription = this.comDescription;
result.mutationDate = this.mutationDate;
return result;
}
Code snippet for an Entity Bean’s setData()
public void setData(CompanyStruct struct) {
this.comName = struct.comName;
this.comDescription = struct.comDescription;
this.mutationDate = struct.mutationDate;;
}
Unlike Design Pattern I, where individual get() and set() operations had to be
performed for getting or setting a particular field, Design Pattern II eliminates these
with just a single RPC. Also, now only one transaction is associated with an RPC that
manipulates all the data in one shot. Thus most of the disadvantages associated with
Design Pattern I have been eliminated, except for the modeling of the relationship
between the entity beans.
Though the setData() method sets all the values, the Borland? AppServer?
provides a “tuned” update feature (turned on by default) whereby only the fields that
are actually modified are written to the database. If no fields were modified, then
the ejbStore() is skipped. Refer to the Borland? Programmer’s guide for EJBs
for further details regarding tuned updates.
Also, there is code repetition in the Entity Bean and the Struct, i.e. the same fields
are declared in both classes. This means that any change to the database table
structure will mean a change to both the Entity Bean and the Struct. Code
maintenance becomes difficult as the Struct and the Entity Bean fields have to be
synchronized.
A small improvement can be made to the size of the code in Design Pattern II by
having the ejbCreate() call the setData() method, thus removing some
redundant code.
Code snippet for an Entity Bean’s create()
public Integer ejbCreate(CompanyStruct struct) throws
CreateException {
this.comId = struct.comId;
//set the primary key
setData(struct);//this removes some redundant code
return null;
}
To avoid the disadvantages associated with Design Pattern I, we introduce the
concept of a value object that encapsulates all the data values associated with an
Entity Bean. The value object, in FoliQuest? terminology, has a “Struct”
appended to it because of their similarity to CORBA Structs.
Value Object code snippet for Company
public class CompanyStruct implements
java.io.Serializable {
public Integer comId;
//Primary Key
public String comName;
public String comDescription;
public java.sql.Timestamp mutationDate;
}
Value Object code snippet for Employee
public class EmployeeStruct implements
java.io.Serializable {
public Integer empId;
//Primary Key
public Integer comId;
//Foreign Key
public String empFirstName;
public String empLastName;
public java.sql.Timestamp mutationDate;
}
Now, the Company and Employee Entity Bean can accept the above Struct as a
parameter for the ejbCreate(). Since the Struct also encapsulates all the
data associated with an Entity Bean, only one getData() and a setData()
method is required to get and set all the fields in the Entity Bean.
Code snippet for an Entity Bean’s create()
public Integer ejbCreate(CompanyStruct struct) throws
CreateException {
this.comId = struct.comId;
this.comName = struct.comName;
this.comDescription = struct.comDescription;
this.mutationDate = struct.mutationDate;
return null;
}
Code snippet for an Entity Bean’s getData()
public CompanyStruct getData() {
CompanyStruct result = new CompanyStruct();
result.comId = this.comId;
result.comName = this.comName;
result.comDescription = this.comDescription;
result.mutationDate = this.mutationDate;
return result;
}
Code snippet for an Entity Bean’s setData()
public void setData(CompanyStruct struct) {
this.comName = struct.comName;
this.comDescription = struct.comDescription;
this.mutationDate = struct.mutationDate;;
}
Unlike Design Pattern I, where individual get() and set() operations had to be
performed for getting or setting a particular field, Design Pattern II eliminates these
with just a single RPC. Also, now only one transaction is associated with an RPC that
manipulates all the data in one shot. Thus most of the disadvantages associated with
Design Pattern I have been eliminated, except for the modeling of the relationship
between the entity beans.
Though the setData() method sets all the values, the Borland? AppServer?
provides a “tuned” update feature (turned on by default) whereby only the fields that
are actually modified are written to the database. If no fields were modified, then
the ejbStore() is skipped. Refer to the Borland? Programmer’s guide for EJBs
for further details regarding tuned updates.
Also, there is code repetition in the Entity Bean and the Struct, i.e. the same fields
are declared in both classes. This means that any change to the database table
structure will mean a change to both the Entity Bean and the Struct. Code
maintenance becomes difficult as the Struct and the Entity Bean fields have to be
synchronized.
A small improvement can be made to the size of the code in Design Pattern II by
having the ejbCreate() call the setData() method, thus removing some
redundant code.
Code snippet for an Entity Bean’s create()
public Integer ejbCreate(CompanyStruct struct) throws
CreateException {
this.comId = struct.comId;
//set the primary key
setData(struct);//this removes some redundant code
return null;
}