YouTip LogoYouTip

Composite Entity Pattern

Composite Entity Pattern

The Composite Entity Pattern is used in the EJB persistence mechanism. A composite entity is an EJB entity bean that represents a graph of objects. When a composite entity is updated, the internal dependent object beans are automatically updated because they are managed by the EJB entity bean. Here are the participants of the composite entity bean.

  • Composite Entity - It is the primary entity bean. It can be coarse-grained or can contain a coarse-grained object for persistent lifecycle.
  • Coarse-Grained Object - This object contains dependent objects. It has its own lifecycle and can also manage the lifecycle of dependent objects.
  • Dependent Object - A dependent object is an object whose persistent lifecycle depends on the coarse-grained object.
  • Strategies - Strategies represent how the composite entity is implemented.

Summary

Intent

Convert tables in a database into composite objects in an application, which can represent a single record or a set of records in the table.

Problem Solved

  • Solves how to efficiently represent and manage complex data structures in a database within object-relational mapping, especially when one-to-many or many-to-many relationships exist.

Use Cases

  • When database tables have complex relationships, or when database tables need to be mapped to composite objects in an application.

Implementation

  • Composite Entity Class: Represents a table in the database, can contain a single record or a set of records.
  • Leaf Entity Class: Represents a single record in the table.
  • Container Entity Class: Represents a set of records in the table, can contain other leaf or container entities.

Key Code

  • Composite Entity Class: Contains methods to add, remove, and access child entities.
  • Leaf Entity Class: Implements the composite entity interface, represents a single record.
  • Container Entity Class: Implements the composite entity interface, can contain multiple leaf or container entities.

Application Example

  • Organizational Structure: An employee can be a leaf node, representing a single employee; a department can be a container node, containing multiple employees.

Advantages

  • Simplifies Data Access: Simplifies access and management of database records through an object-oriented approach.
  • Improves Code Readability: Makes code closer to natural language, easier to understand and maintain.
  • Easy to Extend: New entity types can be conveniently added or existing entities modified.

Disadvantages

  • Performance Issues: May encounter performance bottlenecks when processing large amounts of data.
  • Complexity: May introduce unnecessary complexity for simple data models.

Usage Suggestions

  • Consider using the Composite Entity Pattern when you need to represent and manage complex data structures in a database.

Considerations

  • Ensure that the use of the Composite Entity Pattern does not introduce performance issues, especially when processing large amounts of data.

Main Roles Involved

  • Composite Entity:
    • Represents a table in the database, can contain a single or multiple records.
  • Leaf Entity:
    • Represents a single record in the table, does not contain child entities.
  • Container Entity:
    • Represents a set of records in the table, can contain other leaf or container entities.
  • Data Access Object (DAO) (Optional):
    • Used to access and manipulate the database, encapsulating data access logic.
  • Client (Optional):
    • Uses the Composite Entity Pattern to access and manipulate database records.

The Composite Entity Pattern provides an intuitive and flexible way to handle complex data relationships by mapping database tables to objects in an application.


Implementation

We will create a CompositeEntity object that acts as the composite entity. CoarseGrainedObject is a class that contains dependent objects.

CompositeEntityPatternDemo, our demo class, uses the Client class to demonstrate the usage of the Composite Entity Pattern.

UML Diagram for Composite Entity Pattern

Step 1

Create dependent objects.

DependentObject1.java

public class DependentObject1{
    private String data;

    public void setData(String data){
        this.data = data;
    }

    public String getData(){
        return data;
    }
}

DependentObject2.java

public class DependentObject2{
    private String data;

    public void setData(String data){
        this.data = data;
    }

    public String getData(){
        return data;
    }
}

Step 2

Create the coarse-grained object.

CoarseGrainedObject.java

public class CoarseGrainedObject{
    DependentObject1 do1 = new DependentObject1();
    DependentObject2 do2 = new DependentObject2();

    public void setData(String data1, String data2){
        do1.setData(data1);
        do2.setData(data2);
    }

    public String[] getData(){
        return new String[]{do1.getData(), do2.getData()};
    }
}

Step 3

Create the composite entity.

CompositeEntity.java

public class CompositeEntity{
    private CoarseGrainedObject cgo = new CoarseGrainedObject();

    public void setData(String data1, String data2){
        cgo.setData(data1, data2);
    }

    public String[] getData(){
        return cgo.getData();
    }
}

Step 4

Create the client class that uses the composite entity.

Client.java

public class Client{
    private CompositeEntity compositeEntity = new CompositeEntity();

    public void printData(){
        for(int i = 0; i < compositeEntity.getData().length; i++){
            System.out.println("Data: " + compositeEntity.getData());
        }
    }

    public void setData(String data1, String data2){
        compositeEntity.setData(data1, data2);
    }
}

Step 5

Use the Client to demonstrate the usage of the Composite Entity design pattern.

CompositeEntityPatternDemo.java

public class CompositeEntityPatternDemo{
    public static void main(String[] args){
        Client client = new Client();
        client.setData("Test", "Data");
        client.printData();
        client.setData("Second Test", "Data1");
        client.printData();
    }
}

Step 6

Execute the program, output:

Data: Test
Data: Data
Data: Second Test
Data: Data1
← Data Access Object PatternBusiness Delegate Pattern β†’