`

业务层模式:Composite Entity—复合实体模式

阅读更多

Context
Entity beans are not intended to represent every persistent object in the object model.
Entity beans are better suited for coarse-grained persistent business objects.
Problem
In a Java 2 Platform, Enterprise Edition (J2EE) application, clients -- such as
applications, JavaServer Pages (JSP) pages, servlets, JavaBeans components -- access entity
beans via their remote interfaces. Thus, every client invocation potentially routes through
network stubs and skeletons, even if the client and the enterprise bean are in the same JVM,
OS, or machine. When entity beans are fine-grained objects, clients tend to invoke more
individual entity bean methods, resulting in high network overhead.
Entity beans represent distributed persistent business objects. Whether developing or
migrating an application to the J2EE platform, object granularity is very important when
deciding what to implement as an entity bean. Entity beans should represent coarse-grained
business objects, such as those that provide complex behavior beyond simply getting and
setting field values. These coarse-grained objects typically have dependent objects. A
dependent object is an object that has no real domain meaning when not associated with its
coarse-grained parent.
A recurring problem is the direct mapping of the object model to an Enterprise
JavaBeans (EJB) model (specifically entity beans). This creates a relationship between the
entity bean objects without consideration of coarse-grained versus fine-grained (or
dependent) objects. Determining what to make coarse-grained versus fine-grained is
typically difficult and can best be done via modeling relationships in Unified Modeling
Language (UML) models.
There are a number of areas impacted by the fine-grained entity bean design
approach:
Entity Relationships - Directly mapping an object model to an EJB model does not
take into account the impact of relationships between the objects. The inter-object
relationships are directly transformed into inter-entity bean relationships. As a result, an
entity bean might contain or hold a remote reference to another entity bean. However,
maintaining remote references to distributed objects involves different techniques and
semantics than maintaining references to local objects. Besides increasing the complexity of
the code, it reduces flexibility, because the entity bean must change if there are any changes
in its relationships.
Also, there is no guarantee as to the validity of the entity bean references to other
entity beans over time. Such references are established dynamically using the entity's home
object and the primary key for that entity bean instance. This implies a high maintenance
overhead of reference validity checking for each such entity-bean-to-entity-bean reference.
Manageability - Implementing fine-grained objects as entity beans results in a large
number of entity beans in the system. An entity bean is defined using several classes. For
each entity bean component, the developer must provide classes for the home interface, the
remote interface, the bean implementation, and the primary key.
In addition, the container may generate classes to support the entity bean
implementation. When the bean is created, these classes are realized as real objects in the
container. In short, the container creates a number of objects to support each entity bean
instance. Large numbers of entity beans result in more classes and code to maintain for the
development team. It also results in a large number of objects in the container. This can
negatively impact the application performance.
Network Performance - Fine-grained entity beans potentially have more inter-entity
bean relationships. Entity beans are distributed objects. When one entity bean invokes a
method on another entity bean, the call is potentially treated as a remote call by the
container, even if both entity beans are in the same container or JVM. If the number of
entity-bean-to-entity-bean relationships increases, then this decreases system scalability due
to heavy network overhead.
Database Schema Dependency - When the entity beans are fine-grained, each entity
bean instance usually represents a single row in a database. This is not a proper application
of the entity bean design, since entity beans are more suitable for coarse-grained
components. Fine-grained entity bean implementation typically is a direct representation of
the underlying database schema in the entity bean design. When clients use these
fine-grained entity beans, they are essentially operating at the row level in the database,
since each entity bean is effectively a single row. Because the entity bean directly models a
single database row, the clients become dependent on the database schema. When the
schema changes, the entity bean definitions must change as well. Further, since the clients
are operating at the same granularity, they must observe and react to this change. This
schema dependency causes a loss of flexibility and increases the maintenance overhead
whenever schema changes are required.
Object Granularity (Coarse-Grained versus Fine-Grained) - Object granularity
impacts data transfer between the enterprise bean and the client. In most applications,
clients typically need a larger chunk of data than one or two rows from a table. In such a
case, implementing each of these fine-grained objects as an entity bean means that the client
would have to manage the relationships between all these fine-grained objects. Depending
on the data requirements, the client might have to perform many lookups of a number of
entity beans to obtain the required information.
Forces
Entity beans are best implemented as coarse-grained objects due to the high overhead
associated with each entity bean. Each entity bean is implemented using several objects,
such as EJB home object, remote object, bean implementation, and primary key, and each is
managed by the container services.
Applications that directly map relational database schema to entity beans (where each
row in a table is represented by an entity bean instance) tend to have a large number of
fine-grained entity beans. It is desirable to keep the entity beans coarse-grained and reduce
the number of entity beans in the application.
Direct mapping of object model to EJB model yields fine-grained entity beans.
Fine-grained entity beans usually map to the database schema. This entity-to-database row
mapping causes problems related to performance, manageability, security, and transaction
handling. Relationships between tables are implemented as relationships between entity
beans, which means that entity beans hold references to other entity beans to implement the
fine-grained relationships. It is very expensive to manage inter-entity bean relationships,
because these relationships must be established dynamically, using the entity home objects
and the enterprise beans' primary keys.
Clients do not need to know the implementation of the database schema to use and
support the entity beans. With fine-grained entity beans, the mapping is usually done so that
each entity bean instance maps to a single row in the database. This fine-grained mapping
creates a dependency between the client and the underlying database schema, since the
clients deal with the fine-grained beans and they are essentially a direct representation of
the underlying schema. This results in tight coupling between the database schema and
entity beans. A change to the schema causes a corresponding change to the entity bean, and
in addition requires a corresponding change to the clients.
There is an increase in chattiness of applications due to intercommunication among
fine-grained entity beans. Excessive inter-entity bean communication often leads to a
performance bottleneck. Every method call to the entity bean is made via the network layer,
even if the caller is in the same address space as the called bean (that is, both the client, or
caller entity bean, and the called entity bean are in the same container). While some
container vendors optimize for this scenario, the developer cannot rely on this optimization
in all containers.
Additional chattiness can be observed between the client and the entity beans because
the client may have to communicate with many fine-grained entity beans to fulfill a
requirement. It is desirable to reduce the communication between or among entity beans
and to reduce the chattiness between the client and the entity bean layer.
Solution
Use Composite Entity to model, represent, and manage a set of interrelated persistent
objects rather than representing them as individual fine-grained entity beans. A Composite
Entity bean represents a graph of objects.
In order to understand this solution, let us first define what is meant by persistent
objects and discuss their relationships.
A persistent object is an object that is stored in some type of data store. Multiple
clients usually share persistent objects. Persistent objects can be classified into two types:
coarse-grained objects and dependent objects.
A coarse-grained object is self-sufficient. It has its own life cycle and manages its
relationships to other objects. Each coarse-grained object may reference or contain one or
more other objects. The coarse-grained object usually manages the lifecycles of these
objects. Hence, these objects are called dependent objects. A dependent object can be a
simple self-contained object or may in turn contain other dependent objects.
The life cycle of a dependent object is tightly coupled to the life cycle of the
coarse-grained object. A client may only indirectly access a dependent object through the
coarse-grained object. That is, dependent objects are not directly exposed to clients because
their parent (coarse-grained) object manages them. Dependent objects cannot exist by
themselves. Instead, they always need to have their coarse-grained (or parent) object to
justify their existence.
Typically, you can view the relationship between a coarse-grained object and its
dependent objects as a tree. The coarse-grained object is the root of the tree (the root node).
Each dependent object can be a standalone dependent object (a leaf node) that is a child of
the coarse-grained object. Or, the dependent object can have parent-child relationships with
other dependent objects, in which case it is considered a branch node.
A Composite Entity bean can represent a coarse-grained object and all its related
dependent objects. Aggregation combines interrelated persistent objects into a single entity
bean, thus drastically reducing the number of entity beans required by the application. This
leads to a highly coarse-grained entity bean that can better leverage the benefits of entity
beans than can fine-grained entity beans.
Without the Composite Entity approach, there is a tendency to view each
coarse-grained and dependent object as a separate entity bean, leading to a large number of
entity bean

分享到:
评论

相关推荐

    组合实体模式

    这种模式允许开发者通过更新一个复合实体(Composite Entity),即一个EJB实体Bean,来自动更新其内部依赖的对象Beans。这不仅简化了数据管理和事务处理,还提高了代码的可维护性和扩展性。 #### 二、关键概念与...

    ha-composite-tracker:家庭助理复合设备跟踪器

    每当被观察实体之一更新时,它将更新,并从更改实体中获取last_seen / last_updated(可能还有GPS和电池)数据。 如果“输入”设备跟踪器的更新不定期,则结果可能是更准确和最新的设备跟踪器。 当前支持具有...

    设计模式.docx

    组合实体模式用于将多个实体组合在一起,形成一个复合实体。这种模式可以帮助管理复杂的业务对象结构。 #### 数据访问对象模式(Data Access Object Pattern) 数据访问对象(DAO)模式提供了一种机制,用于将底层...

    ofbiz开源框架学习资料(附有小例子)

    - **设计模式**:在实体引擎中,OFBIZ采用了多种设计模式,如业务代理(Business Delegate)、值对象(Value Object)、复合实体(Composite Entity)、值对象组装器(Value Object Assembler)、服务定位器(Service ...

    Composite-datatype-example-master_cuba_Cubaplatfrom_project_modu

    1. **定义复合数据类型**:通过创建复合实体,将多个属性封装到一个类型中,这些属性可以是基本类型或引用其他实体。这可以通过创建一个新的Java类来实现,该类包含所需的属性并继承自`...

    J2EE Patterns

    - **复合实体**(Composite Entity):组合多个实体对象。 - **传输对象**(Transfer Object):用于数据交换的对象。 - **传输对象组装器**(Transfer Object Assembler):组装传输对象。 - **值列表处理器**...

    hibernate复合主键映射

    在Java的持久化框架Hibernate中,复合主键(Composite Key)是一种特殊的数据模型,它用于表示由多个属性共同构成的唯一标识。这种设计通常出现在实体类的某些属性组合起来才能唯一确定一个对象的情况下。本篇文章将...

    hibernate复合主键的实例

    在实际开发中,根据具体需求调整实体和复合主键类的属性,以满足业务场景。 四、总结 复合主键在多字段标识的情况下非常有用,它可以保证数据的完整性。在Hibernate中,通过定义`@Embeddable`的复合主键类和`@...

    数据库课件总结:Database Chapter Six Outline.docx

    - **简单属性**(Simple Attributes)与**复合属性**(Composite Attributes):简单属性是指不能再分解的属性;而复合属性则可以进一步拆分为更简单的属性。 - **单值属性**(Single-valued Attributes)与**多值...

    hibernate复合主键设置

    在业务代码中,创建和操作复合主键的实体对象与普通实体对象并无太大区别。只需要确保在创建新的`OrderItem`实例时,为其`id`属性提供正确的`OrderItemId`实例即可。 总结,复合主键在Hibernate中是通过`@...

    hibernate复合主键配置和使用

    在代码中,我们可以像操作普通实体一样保存和查询复合主键的实体。例如,添加新记录: ```java Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); AddressId ...

    数据库管理系统概述英文版课件:2 ER Diagram.ppt

    Weak Entity(弱实体)和 Strong Entity(强实体) 弱实体是依赖于其他实体存在的实体,而强实体则可以独立存在。例如,一个订单的实体是一个弱实体,因为它依赖于客户的实体。 Class Hierarchy(类继承) 类继承...

    Openjpa手册

    - **实体身份**:实体的身份可以通过主键(Primary Key)或者复合主键(Composite Primary Key)来确定。 - **主键类**:如果实体使用复合主键,则需要定义一个主键类。 - **主键层次结构**:在继承场景下,主键的...

    Hibernate复合主键.

    然而,对于复合主键,一般不推荐使用自增策略,因为复合主键通常由业务数据决定,而不是自动递增。 四、复合主键的持久化操作 在进行增删改查操作时,由于复合主键的存在,我们需要确保所有关联的主键值都被正确...

    openJPA官方手册

    - **架构概述**:JPA提供了一套统一的API来处理不同的持久化存储层,这包括实体管理器(EntityManager)、持久化单元(Persistence Unit)等核心概念。 #### 四、实体(Entity) - **限制条件**:在定义持久化类时...

    数据库ER图PPT

    - **复合属性**(Composite Attributes):指可以进一步分解成更小单元的属性。 - **单值属性**(Single-Valued Attributes):每个实体实例只有一个值的属性。 - **多值属性**(Multi-Valued Attributes):每个...

Global site tag (gtag.js) - Google Analytics