V
VRGL
Unregistered / Unconfirmed
GUEST, unregistred user!
Design Pattern IV
In Design Pattern III, we saw that the size of the Entity Bean was greatly reduced by
having the Entity Bean inherit from the Struct and thereby making all the fields
(columns in the database) available for CMP. Here we further reduce the size of the
Entity Bean in the implementation of the setData() and getData() methods
by introducing an additional method in the Struct.
Value Object code snippet for Company
public class CompanyStruct implements
java.io.Serializable {
public Integer comId;
public String comName;
public String comDescription;
public Timestamp mutationDate;
public void copyFrom(CompanyStruct struct) {
comId = struct.comId;
comName = struct.comName;
comDescription = struct.comDescription;
mutationDate = struct.mutationDate;
}
}
Since Entity Bean inherits from the Struct defined above, the copyFrom()
method is available in the implementation class. However it should be noted that the
copyFrom() method is not a business method i.e. it is not exposed in the remote
interface of the entity bean. Now the getData() and setData() methods in
the Entity Bean’s implementation class in Design Patterns II and III can be reduced
to:
Code snippet for an Entity Bean’s getData()
public CompanyStruct getData() {
CompanyStruct result = new CompanyStruct();
result.copyFrom(this);
return result;
}
The “this” passed as a parameter to the copyFrom() method refers to the
Entity Bean implementation class. Since it inherits from the Struct, the entity bean
implementation class passed as a parameter to the copyFrom() method is actually
cast to the Struct class. EJB? vendors frown upon passing “this” as a parameter
because two threads of control could have a reference to the same bean and a head-on
collision could occur with transaction contexts when a method is invoked on it.
What we havedo
ne above is not in violation of the EJB? vendor’s suggestion as
the reference is not passed between beans and no methods are called on the
reference that could result in a transaction collision.
Code snippet for an Entity Bean’s setData()
public void setData(CompanyStruct struct) {
this.copyFrom(struct);
}
Similarly, the above method copies the values from the Struct to the Entity Bean.
With an Entity Bean that maps to a table with a large number of columns, the
advantages are extremely high with respect to the size of code in the Entity Bean
implementation class. The above design pattern makes the code extremely compact,
easy to read, understand and maintain.
Also, any change to the Database table requires only a change in the Struct. Hardly
(if any) code needs to be changed in the Entity Bean implementation class. Apart
from this change to the Struct, the Deployment Descriptor needs to be changed
when CMP fields are added or removed thus improving your team’s response to
normal development as well as changes in specifications etc.
At this stage, the relationship between the Company and Employee Entity Bean is
still not modeled. This isdo
ne in Design Pattern V.
In Design Pattern III, we saw that the size of the Entity Bean was greatly reduced by
having the Entity Bean inherit from the Struct and thereby making all the fields
(columns in the database) available for CMP. Here we further reduce the size of the
Entity Bean in the implementation of the setData() and getData() methods
by introducing an additional method in the Struct.
Value Object code snippet for Company
public class CompanyStruct implements
java.io.Serializable {
public Integer comId;
public String comName;
public String comDescription;
public Timestamp mutationDate;
public void copyFrom(CompanyStruct struct) {
comId = struct.comId;
comName = struct.comName;
comDescription = struct.comDescription;
mutationDate = struct.mutationDate;
}
}
Since Entity Bean inherits from the Struct defined above, the copyFrom()
method is available in the implementation class. However it should be noted that the
copyFrom() method is not a business method i.e. it is not exposed in the remote
interface of the entity bean. Now the getData() and setData() methods in
the Entity Bean’s implementation class in Design Patterns II and III can be reduced
to:
Code snippet for an Entity Bean’s getData()
public CompanyStruct getData() {
CompanyStruct result = new CompanyStruct();
result.copyFrom(this);
return result;
}
The “this” passed as a parameter to the copyFrom() method refers to the
Entity Bean implementation class. Since it inherits from the Struct, the entity bean
implementation class passed as a parameter to the copyFrom() method is actually
cast to the Struct class. EJB? vendors frown upon passing “this” as a parameter
because two threads of control could have a reference to the same bean and a head-on
collision could occur with transaction contexts when a method is invoked on it.
What we havedo
ne above is not in violation of the EJB? vendor’s suggestion as
the reference is not passed between beans and no methods are called on the
reference that could result in a transaction collision.
Code snippet for an Entity Bean’s setData()
public void setData(CompanyStruct struct) {
this.copyFrom(struct);
}
Similarly, the above method copies the values from the Struct to the Entity Bean.
With an Entity Bean that maps to a table with a large number of columns, the
advantages are extremely high with respect to the size of code in the Entity Bean
implementation class. The above design pattern makes the code extremely compact,
easy to read, understand and maintain.
Also, any change to the Database table requires only a change in the Struct. Hardly
(if any) code needs to be changed in the Entity Bean implementation class. Apart
from this change to the Struct, the Deployment Descriptor needs to be changed
when CMP fields are added or removed thus improving your team’s response to
normal development as well as changes in specifications etc.
At this stage, the relationship between the Company and Employee Entity Bean is
still not modeled. This isdo
ne in Design Pattern V.