Composite Entity Design Pattern
Start course
3h 59m

In this course, we will learn the concepts of microservice and spring framework with a focus on design patterns.

Learning Objectives

  • Learn about various design patterns

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud


  • Basic Java knowledge

Hello dear friends. In this video, we will examine the composite entity pattern. So, let's begin. As described by GoF, composite entity pattern composes objects into tree structure to represent part whole hierarchies. Composite lets client treat individual objects and compositions of objects uniformly. In object-oriented programming, a composite is an object designed as a composition of one or more similar objects, all exhibiting similar functionality. This is known as a 'has a relationship' between objects. Composite entity is a Java EE software design pattern and it's 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. Composite entity pattern is used in EJB persistence mechanism. A composite entity is an EJB entity bean which represents a graph of objects. When a composite entity is updated, internally dependent object beans get updated automatically as bean managed by EJB entity bean. As you can see in the UML of composite entity pattern, there are four participants. CompositeEntity is the primary entity bean. It can be coarse-grained or can contain a coarse-grained object to be used for persistence purposes. CoarseGrainedObject contains dependent objects.

It has its own lifecycle and also manages the lifecycle of dependent objects. DependentObject is an object which depends on CoarseGrainedObject for its persistence lifecycle. Strategies represent how to implement a composite entity. Now that we've learned the structure of composite entity pattern, let's create a project and implement the pattern. Create a new project and name it CompositeEntity. I'd like to use the employee example again. Let's define two classes which feature data that would need to update another class. First one is Employee. Define name and jobState. Generate getter and setter for jobState. Now, let's add a manager. In this, we will define name and satisfaction. If the employee does well, the manager is satisfied and vice versa. Generate getter and setter for satisfaction.

Since the point of this pattern is to not allow the beans to act as fine-grained objects alone, we're introduced with the CoarseGrainedObject. This object manages its own relationships to other objects. In CoarseGrainedObject class we will use employee and manager together. Okay, let's get back to it. Let's define two methods to set data and get data. Okay, done. Now we have to define a composite entity class. This class is itself a coarse grained object and can reference another. First, define CoarseGrainedObject. Now add getData and setData methods. And of course, we need a user as client. Define CompositeEntity. Let's add a print method. Now we will use setData also. We have finished structure, let's use. Now, only we need client. Define client instance, now setData. Print, change, and print again. Now let's look at the result, run. Look if employee is successful, manager is satisfied, else, manager is unsatisfied. All right, with this video, we have finished examining the business to your patterns. In the next video, we will start to look at presentation to your patterns starting with the intercepting filter pattern. So, I'll see you in the next video my friends.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.