Jump to content
Wikipedia The Free Encyclopedia

Composite entity pattern

From Wikipedia, the free encyclopedia

Composite entity is a Java EE Software design pattern and it is used to model, represent, and manage a set of interrelated persistent objects rather than representing them as individual fine-grained entity beans, and also a composite entity bean represents a graph of objects.[1]

Structure

[edit ]

There are a number of strategies to implement composite entity pattern. This pattern mainly composites of composite entity, coarse-grained object, and dependent objects.[1]

Composite entity pattern class diagram
Composite entity pattern class diagram


Composite entity component

[edit ]

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.[1]

Coarse-grained object

[edit ]

A coarse-grained object is an object with its own life cycle manages its own relationships to other objects. It can be an object contained in the composite entity, or, composite entity itself can be the coarse-grained object which holds dependent objects.[1]

Dependent objects

[edit ]

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.[1]

Consequences

[edit ]

According to Oracle description of the pattern, consequences include eliminating inter-entity relationships, improving manageability by reducing entity beans, improving network performance, reducing database schema dependency, increasing object granularity, facilitating composite transfer object creation and overhead of multi-level dependent object graphs.[1]

Drawbacks

[edit ]

The fatal drawback is the requirement of bean-managed persistent (BMP) bean. This involves more work for developers, and create some problems as follows:

  • materializing all the data in a coarse-grained entity whenever it is accessed, is unacceptably expensive
  • In 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 container managed persistence (CMP) beans. These both approaches reduce the maintainability.[2]

Sample code

[edit ]

Sample code for a Professional Service Automation application (PSA) in which the resource object is implemented via composite entity pattern, may look like as follows (entity implements coarse-grained object):

packagecorepatterns.apps.psa.ejb;
importcorepatterns.apps.psa.core.*;
importcorepatterns.apps.psa.dao.*;
importjava.sql.*;
importjavax.sql.*;
importjava.util.*;
importjavax.ejb.*;
importjavax.naming.*;
publicclass ResourceEntityimplementsEntityBean{
publicStringemployeeId;
publicStringlastName;
publicStringfirstName;
publicStringdepartmentId;
publicStringpracticeGroup;
publicStringtitle;
publicStringgrade;
publicStringemail;
publicStringphone;
publicStringcell;
publicStringpager;
publicStringmanagerId;

// Collection of BlockOutTime Dependent objects
publicCollectionblockoutTimes;
// Collection of SkillSet Dependent objects
publicCollectionskillSets;
...
privateEntityContextcontext;
// Entity Bean methods implementation
publicStringejbCreate(ResourceTOresource)throws
CreateException{
try{
this.employeeId=resource.employeeId;
setResourceData(resource);
getResourceDAO().create(resource);
}catch(Exceptionex){
thrownewEJBException("Reason:"+...);
}
returnthis.employeeId;
}

publicStringejbFindByPrimaryKey(StringprimaryKey)
throwsFinderException{
booleanresult;
try{
ResourceDAOresourceDAO=getResourceDAO();
result=
resourceDAO.selectByPrimaryKey(primaryKey);
}catch(Exceptionex){
thrownewEJBException("Reason:"+...);
}
if(result){
returnprimaryKey;
}
else{
thrownewObjectNotFoundException(...);
}
}

publicvoidejbRemove(){
try{
// Remove dependent objects
if(this.skillSets!=null){
SkillSetDAOskillSetDAO=getSkillSetDAO();
skillSetDAO.setResourceID(employeeId);
skillSetDAO.deleteAll();
skillSets=null;
}
if(this.blockoutTime!=null){
BlockOutTimeDAOblockouttimeDAO=
getBlockOutTimeDAO();
blockouttimeDAO.setResourceID(employeeId);
blockouttimeDAO.deleteAll();
blockOutTimes=null;
}
// Remove the resource from the persistent store
ResourceDAOresourceDAO=new
ResourceDAO(employeeId);
resourceDAO.delete();
}catch(ResourceExceptionex){
thrownewEJBException("Reason:"+...);
}catch(BlockOutTimeExceptionex){
thrownewEJBException("Reason:"+...);
}catch(Exceptionexception){
...
}
}
publicvoidsetEntityContext(EntityContextcontext)
{
this.context=context;
}

publicvoidunsetEntityContext(){
context=null;
}

publicvoidejbActivate(){
employeeId=(String)context.getPrimaryKey();
}

publicvoidejbPassivate(){
employeeId=null;
}

publicvoidejbLoad(){
try{
// load the resource info from
ResourceDAOresourceDAO=getResourceDAO();
setResourceData((ResourceTO)
resourceDAO.load(employeeId));

// Load other dependent objects, if necessary
...
}catch(Exceptionex){
thrownewEJBException("Reason:"+...);
}
}

publicvoidejbStore(){
try{
// Store resource information
getResourceDAO().update(getResourceData());
// Store dependent objects as needed
...
}catch(SkillSetExceptionex){
thrownewEJBException("Reason:"+...);
}catch(BlockOutTimeExceptionex){
thrownewEJBException("Reason:"+...);
}
...
}
publicvoidejbPostCreate(ResourceTOresource){
}
// Method to Get Resource Transfer Object
publicResourceTOgetResourceTO(){
// create a new Resource Transfer Object
ResourceTOresourceTO=new
ResourceTO(employeeId);
// copy all values 
resourceTO.lastName=lastName;
resourceTO.firstName=firstName;
resourceTO.departmentId=departmentId;
...
returnresourceTO;
}
publicvoidsetResourceData(ResourceTOresourceTO){
// copy values from Transfer Object into entity bean
employeeId=resourceTO.employeeId;
lastName=resourceTO.lastName;
...
}
// Method to get dependent Transfer Objects
publicCollectiongetSkillSetsData(){
// If skillSets is not loaded, load it first.
// See Lazy Load strategy implementation.
returnskillSets;
}
...
// other get and set methods as needed
...
// Entity bean business methods
publicvoidaddBlockOutTimes(CollectionmoreBOTs)
throwsBlockOutTimeException{
// Note: moreBOTs is a collection of 
// BlockOutTimeTO objects
try{
IteratormoreIter=moreBOTs.iterator();
while(moreIter.hasNext()){
BlockOutTimeTObotTO=(BlockOutTimeTO)
moreIter.next();
if(!(blockOutTimeExists(botTO))){
// add BlockOutTimeTO to collection
botTO.setNew();
blockOutTime.add(botTO);
}else{
// BlockOutTimeTO already exists, cannot add
thrownewBlockOutTimeException(...);
}
}
}catch(Exceptionexception){
thrownewEJBException(...);
}
}
publicvoidaddSkillSet(CollectionmoreSkills)
throwsSkillSetException{
// similar to addBlockOutTime() implementation
...
}
...
publicvoidupdateBlockOutTime(CollectionupdBOTs)
throwsBlockOutTimeException{
try{
IteratorbotIter=blockOutTimes.iterator();
IteratorupdIter=updBOTs.iterator();
while(updIter.hasNext()){
BlockOutTimeTObotTO=(BlockOutTimeTO)
updIter.next();
while(botIter.hasNext()){
BlockOutTimeTOexistingBOT=
(BlockOutTimeTO)botIter.next();
// compare key values to locate BlockOutTime
if(existingBOT.equals(botTO)){
// Found BlockOutTime in collection
// replace old BlockOutTimeTO with new one
botTO.setDirty();//modified old dependent
botTO.resetNew();//not a new dependent
existingBOT=botTO;
}
}
}
}catch(Exceptionexc){
thrownewEJBException(...);
}
}
publicvoidupdateSkillSet(CollectionupdSkills)
throwsCommitmentException{
// similar to updateBlockOutTime...
...
}
...
}

[1]

See also

[edit ]

References

[edit ]
  1. ^ a b c d e f g "Core J2EE Patterns - Composite Entity". Oracle. Oracle. Retrieved 6 February 2016.
  2. ^ Johnson, R. (2003). Expert One-on-One J2EE Design and Development. Indianapolis: Wiley Publishing, Inc. p. 290.
Gang of Four
patterns
Creational
Structural
Behavioral
Concurrency
patterns
Architectural
patterns
Other
patterns
Books
People
Communities
See also

AltStyle によって変換されたページ (->オリジナル) /