Content deleted Content added
Shevonsilva (talk | contribs) No edit summary |
|||
(14 intermediate revisions by 12 users not shown) | |||
Line 1:
'''Composite entity''' is a [[Java Platform, Enterprise Edition|Java EE]] [[Software design pattern]] and it is used to model, represent, and manage a set of interrelated persistent objects rather
==Structure==
[[File:Composte entity pattern class diagram.png|center|Composite entity pattern class diagram]]
<!-- Deleted image removed: [[File:Composite entity pattern sequence diagram.png|center|Composite entity pattern sequence diagram]] -->
===Composite entity component===
Composite entity is the coarse-grained entity bean which may be the coarse-grained object, or may contain a reference to the coarse-grained object.<ref name=":o_cjp"/>
===Coarse-grained object===
A coarse-grained object is an object with its own life
===Dependent objects===
It is an object, which can contain other dependent objects (there may be a tree of objects within the composite entity), that depends on the coarse-grained object and has its life cycle managed by the coarse-grained object.<ref name=":o_cjp"/>
==Consequences==
According to Oracle description of the pattern, consequences
===Drawbacks===
The fatal drawback is the requirement of bean-managed persistent (BMP) [[JavaBeans|bean]]. This involves more work for developers, and
* materializing all the data in a coarse-grained entity whenever it
* In [[Java (programming language)|Java]], implementation of the ejbStore() method needs to be smart enough to avoid issuing all the updates required to persist the entire state of the object, unless the data has changed in all the persistent objects.
Composite entity pattern can only be implemented using BMP or by adding more hand-coded persistence logic to
==Sample code==
Sample code for a Professional Service Automation application (PSA) in which the resource object is
<
package corepatterns.apps.psa.ejb;
Line 31 ⟶ 40:
public class ResourceEntity implements EntityBean {
public String employeeId;
public String lastName;
public String firstName;
public String departmentId;
public String practiceGroup;
public String title;
public String grade;
public String email;
public String phone;
public String cell;
public String pager;
public String managerId;
// Collection of BlockOutTime Dependent objects
public Collection blockoutTimes;
// Collection of SkillSet Dependent objects
public Collection skillSets;
...
private EntityContext context;
// Entity Bean methods implementation
public String ejbCreate(ResourceTO resource) throws
CreateException {
try {
this.employeeId = resource.employeeId;
setResourceData(resource);
getResourceDAO().create(resource);
} catch (Exception ex) {
throw new EJBException("Reason:" + ...);
}
return this.employeeId;
}
public String ejbFindByPrimaryKey(String primaryKey)
throws FinderException {
boolean result;
try {
ResourceDAO resourceDAO = getResourceDAO();
result =
resourceDAO.selectByPrimaryKey(primaryKey);
} catch (Exception ex) {
throw new EJBException("Reason:" + ...);
}
if (result) {
return primaryKey;
}
else {
throw new ObjectNotFoundException(...);
}
}
public void ejbRemove() {
try {
// Remove dependent objects
if (this.skillSets != null) {
SkillSetDAO skillSetDAO = getSkillSetDAO();
skillSetDAO.setResourceID(employeeId);
skillSetDAO.deleteAll();
skillSets = null;
}
if (this.blockoutTime != null) {
BlockOutTimeDAO blockouttimeDAO =
getBlockOutTimeDAO();
blockouttimeDAO.setResourceID(employeeId);
blockouttimeDAO.deleteAll();
blockOutTimes = null;
}
// Remove the resource from the persistent store
ResourceDAO resourceDAO = new
ResourceDAO(employeeId);
resourceDAO.delete();
} catch (ResourceException ex) {
throw new EJBException("Reason:"+...);
} catch (BlockOutTimeException ex) {
throw new EJBException("Reason:"+...);
} catch (Exception exception) {
...
}
}
public void setEntityContext(EntityContext context)
{
this.context = context;
}
public void unsetEntityContext() {
context = null;
public void ejbActivate() {
employeeId = (String)context.getPrimaryKey();
}
public void ejbPassivate() {
employeeId = null;
}
public void ejbLoad() {
try {
// load the resource info from
ResourceDAO resourceDAO = getResourceDAO();
setResourceData((ResourceTO)
resourceDAO.load(employeeId));
// Load other dependent objects, if necessary
...
} catch (Exception ex) {
throw new EJBException("Reason:" + ...);
}
}
public void ejbStore() {
try {
// Store resource information
getResourceDAO().update(getResourceData());
// Store dependent objects as needed
...
} catch (SkillSetException ex) {
throw new EJBException("Reason:" + ...);
} catch (BlockOutTimeException ex) {
throw new EJBException("Reason:" + ...);
}
}
public void ejbPostCreate(ResourceTO resource) {
}
public ResourceTO getResourceTO() {
// create a new Resource Transfer Object
ResourceTO resourceTO = new
ResourceTO(employeeId);
// copy all values
resourceTO.lastName = lastName;
resourceTO.firstName = firstName;
resourceTO.departmentId = departmentId;
...
return resourceTO;
}
public void setResourceData(ResourceTO resourceTO) {
// copy values from Transfer Object into entity bean
employeeId = resourceTO.employeeId;
lastName = resourceTO.lastName;
}
// Method to get dependent Transfer Objects
public Collection getSkillSetsData() {
// If skillSets is not loaded, load it first.
// See Lazy Load strategy implementation.
return skillSets;
}
...
// other get and set methods as needed
...
// Entity bean business methods
public
throws BlockOutTimeException {
// Note: moreBOTs is a collection of
// BlockOutTimeTO objects
try {
Iterator moreIter = moreBOTs.iterator();
while (moreIter.hasNext()) {
BlockOutTimeTO botTO = (BlockOutTimeTO)
moreIter.next();
if (! (blockOutTimeExists(botTO))) {
// add BlockOutTimeTO to collection
botTO.setNew();
blockOutTime.add(botTO);
} else {
// BlockOutTimeTO already exists, cannot add
throw new BlockOutTimeException(...);
}
}
} catch (Exception exception) {
throw new EJBException(...);
}
}
public void addSkillSet(Collection moreSkills)
throws SkillSetException {
// similar to addBlockOutTime() implementation
...
}
...
public void updateBlockOutTime(Collection updBOTs)
throws BlockOutTimeException {
try {
Iterator botIter = blockOutTimes.iterator();
Iterator updIter = updBOTs.iterator();
BlockOutTimeTO botTO = (BlockOutTimeTO)
// compare key values to locate BlockOutTime
// replace old BlockOutTimeTO with new one
botTO.setDirty(); //modified old dependent
}
} catch (Exception exc) {
throw new EJBException(...);
}
}
public void updateSkillSet(Collection updSkills)
throws CommitmentException {
// similar to updateBlockOutTime...
...
}
...
}
</syntaxhighlight><ref name=":o_cjp"/>
==See also==
* [[Data transfer object]]
Line 272 ⟶ 280:
{{Design Patterns patterns}}
[[Category:Software design patterns]]
[[Category:Articles with example Java code]]
|