Composite Entity Pattern
The Composite Entity Pattern is used in EJB persistence mechanisms. A composite entity is an EJB entity bean that represents a graph of objects. When a composite entity is updated, the internally dependent object beans are updated automatically, because they are managed by the EJB entity bean. The following are the participants of a composite entity bean.
- Composite Entity - It is the main entity bean. It can be coarse-grained, or it can contain a coarse-grained object for a persistent lifecycle.
- Coarse-Grained Object - This object contains dependent objects. It has its own lifecycle and can also manage the lifecycles of the dependent objects.
- Dependent Object - A dependent object is an object whose lifecycle depends on the coarse-grained object.
- Strategies - Strategies represent how the composite entity is implemented.
Summary
Intent
Convert tables in the database into composite objects in the application; these objects can represent a single record or a group of records in a table.
Main problem solved
- Solve the problem of how to efficiently represent and manage complex data structures in a database in 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 the application.
Implementation
- Composite Entity class: represents a table in the database and can contain a single record or a group of records.
- Leaf Entity class: represents a single record in a table.
- Container Entity class: represents a group of records in a table and 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, representing a single record.
- Container Entity class: implements the Composite Entity interface and 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
- Simplified data access: simplifies the access and management of database records through objects.
- Improve code readability: makes the code closer to natural language, easy to understand and maintain.
- Easy to extend: makes it easy to add new entity types or modify existing entities.
Disadvantages
- Performance Issues: when handling large amounts of data, performance bottlenecks may be encountered.
- Complexity: for simple data models, it may add unnecessary complexity.
Usage recommendation
- When you need to represent and manage complex data structures in a database, consider using the Composite Entity pattern.
Notes
- Ensure that using the Composite Entity pattern does not introduce performance issues, especially when handling large amounts of data.
Main roles included
-
Composite Entity:
- Represents a table in the database and can contain a single record or multiple records.
-
Leaf Entity:
- Represents a single record in a table and does not contain child entities.
-
Container Entity:
- Represents a group of records in a table and can contain other leaf or container entities.
-
Data Access Object (optional):
- Used to access and manipulate the database, encapsulating the 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 the application.
Create a class that implements
We will create the CompositeEntity object.CoarseGrainedObject which is a class containing dependent objects.
CompositeEntityPatternDemo, and our demo class uses the Client class to demonstrate the use of the Composite Entity pattern.
Step 1
Create the dependent object.
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
(
)
[
i
]
)
;
}
}
public
void
setData
(
String
data1
,
String
data2
)
{
compositeEntity
.
setData
(
data1
,
data2
)
;
}
}
Step 5
Use Client to demonstrate the use 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
Run the program, output result:
Data: Test
Data: Data
Data: Second Test
Data: Data1