Composite entity is a Java EESoftware 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
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 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.[1]
Coarse-grained object
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
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
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
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
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.*;publicclassResourceEntityimplementsEntityBean{publicStringemployeeId;publicStringlastName;publicStringfirstName;publicStringdepartmentId;publicStringpracticeGroup;publicStringtitle;publicStringgrade;publicStringemail;publicStringphone;publicStringcell;publicStringpager;publicStringmanagerId;// Collection of BlockOutTime Dependent objectspublicCollectionblockoutTimes;// Collection of SkillSet Dependent objectspublicCollectionskillSets;...privateEntityContextcontext;// Entity Bean methods implementationpublicStringejbCreate(ResourceTOresource)throwsCreateException{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 objectsif(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 storeResourceDAOresourceDAO=newResourceDAO(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 fromResourceDAOresourceDAO=getResourceDAO();setResourceData((ResourceTO)resourceDAO.load(employeeId));// Load other dependent objects, if necessary...}catch(Exceptionex){thrownewEJBException("Reason:"+...);}}publicvoidejbStore(){try{// Store resource informationgetResourceDAO().update(getResourceData());// Store dependent objects as needed...}catch(SkillSetExceptionex){thrownewEJBException("Reason:"+...);}catch(BlockOutTimeExceptionex){thrownewEJBException("Reason:"+...);}...}publicvoidejbPostCreate(ResourceTOresource){}// Method to Get Resource Transfer ObjectpublicResourceTOgetResourceTO(){// create a new Resource Transfer ObjectResourceTOresourceTO=newResourceTO(employeeId);// copy all values resourceTO.lastName=lastName;resourceTO.firstName=firstName;resourceTO.departmentId=departmentId;...returnresourceTO;}publicvoidsetResourceData(ResourceTOresourceTO){// copy values from Transfer Object into entity beanemployeeId=resourceTO.employeeId;lastName=resourceTO.lastName;...}// Method to get dependent Transfer ObjectspublicCollectiongetSkillSetsData(){// If skillSets is not loaded, load it first.// See Lazy Load strategy implementation.returnskillSets;}...// other get and set methods as needed...// Entity bean business methodspublicvoidaddBlockOutTimes(CollectionmoreBOTs)throwsBlockOutTimeException{// Note: moreBOTs is a collection of // BlockOutTimeTO objectstry{IteratormoreIter=moreBOTs.iterator();while(moreIter.hasNext()){BlockOutTimeTObotTO=(BlockOutTimeTO)moreIter.next();if(!(blockOutTimeExists(botTO))){// add BlockOutTimeTO to collectionbotTO.setNew();blockOutTime.add(botTO);}else{// BlockOutTimeTO already exists, cannot addthrownewBlockOutTimeException(...);}}}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 BlockOutTimeif(existingBOT.equals(botTO)){// Found BlockOutTime in collection// replace old BlockOutTimeTO with new onebotTO.setDirty();//modified old dependentbotTO.resetNew();//not a new dependentexistingBOT=botTO;}}}}catch(Exceptionexc){thrownewEJBException(...);}}publicvoidupdateSkillSet(CollectionupdSkills)throwsCommitmentException{// similar to updateBlockOutTime......}...}