Oracle® Application Server Containers for J2EE Enterprise JavaBeans Developer's Guide
10g Release 2 (10.1.2) Part No. B15505-01 |
|
![]() Previous |
![]() Next |
The EJB 2.0 specification enables the specification of relationships between entity beans. An entity bean can be defined so as to have a relationship with other entity beans. You implement relationships differently for entity beans with bean-managed-persistence than those entity beans that utilize container-managed-persistence. With bean-managed persistence, the code that you write implements the relationships. With container-managed persistence, the EJB container takes care of the relationships for you. For this reason, relationships in entity beans with container-managed persistence are often referred to as container-managed relationships.
Relationship Fields - A relationship field in an EJB identifies a related bean. A relationship field is virtual and is defined in the enterprise bean class with access methods. Unlike a persistent field, a relationship field does not represent the bean's state.
Multiplicity in Container-Managed Relationships - There are four types of multiplicities all of which are supported by Oracle Application Server:
One-to-One - Each entity bean instance is related to a single instance of another entity bean.
One-to-Many - An entity bean instance is related to multiple instances of the other entity bean.
Many-to-One - Multiple instances of an entity bean may be related to a single instance of the other entity bean. This multiplicity is the opposite of one-to-many.
Many-to-Many - The entity bean instances may be related to multiple instances of each other.
Direction in Container-Managed Relationships - The direction of a relationship may be either bi-directional or unidirectional. In a unidirectional relationship, only one entity bean has a relationship field that refers to the other. In a bi-directional relationship, each entity bean has a relationship field that refers to the other bean. Through the relationship field, an entity bean's code can access its related object. If an entity bean has a relative field, then we often say that it "knows" about its related object. Oracle Application Server supports both unidirectional and bi-directional relationships between EJBs.
EJBQL and CMP With Relationships - EJB QL queries often navigate across relationships. The direction of a relationship determines whether a query can navigate from one bean to another. With Oracle Application Server, EJBQL queries can traverse CMP Relationships with any type of multiplicity and with both unidirectional and bi-directional relationships.
For more information, see Chapter 5, "CMP Entity Beans" for container-managed persistence, Chapter 6, "Entity Relationship Mapping" for CMR relationships, and Chapter 7, "EJB Query Language" for EJB QL.