background image

Direction in Entity Relationships

<< Multiplicity in Entity Relationships | Unidirectional Relationships >>
<< Multiplicity in Entity Relationships | Unidirectional Relationships >>

Direction in Entity Relationships

One-to-one: Each entity instance is related to a single instance of another entity. For example, to
model a physical warehouse in which each storage bin contains a single widget, StorageBin and
Widget
would have a one-to-one relationship. One-to-one relationships use the
javax.persistence.OneToOne
annotation on the corresponding persistent property or field.
One-to-many: An entity instance can be related to multiple instances of the other entities. A
sales order, for example, can have multiple line items. In the order application, Order would
have a one-to-many relationship with LineItem. One-to-many relationships use the
javax.persistence.OneToMany
annotation on the corresponding persistent property or field.
Many-to-one: Multiple instances of an entity can be related to a single instance of the other
entity. This multiplicity is the opposite of a one-to-many relationship. In the example just
mentioned, from the perspective of LineItem the relationship to Order is many-to-one.
Many-to-one relationships use the javax.persistence.ManyToOne annotation on the
corresponding persistent property or field.
Many-to-many: The entity instances can be related to multiple instances of each other. For
example, in college each course has many students, and every student may take several courses.
Therefore, in an enrollment application, Course and Student would have a many-to-many
relationship. Many-to-many relationships use the javax.persistence.ManyToMany
annotation on the corresponding persistent property or field.
Direction in Entity Relationships
The direction of a relationship can be either bidirectional or unidirectional. A bidirectional
relationship has both an owning side and an inverse side. A unidirectional relationship has only
an owning side. The owning side of a relationship determines how the Persistence runtime
makes updates to the relationship in the database.
Bidirectional Relationships
In a bidirectional relationship, each entity has a relationship field or property that refers to the
other entity. Through the relationship field or property, an entity class's code can access its
related object. If an entity has a related field, then the entity is said to "know" about its related
object. For example, if Order knows what LineItem instances it has and if LineItem knows
what Order it belongs to, then they have a bidirectional relationship.
Bidirectional relationships must follow these rules:
The inverse side of a bidirectional relationship must refer to its owning side by using the
mappedBy
element of the @OneToOne, @OneToMany, or @ManyToMany annotation. The
mappedBy
element designates the property or field in the entity that is the owner of the
relationship.
The many side of many-to-one bidirectional relationships must not define the mappedBy
element. The many side is always the owning side of the relationship.
Entities
The Java EE 5 Tutorial · September 2007
690