background image

Unidirectional Relationships

<< Direction in Entity Relationships | Mapped Superclasses >>
<< Direction in Entity Relationships | Mapped Superclasses >>

Unidirectional Relationships

For one-to-one bidirectional relationships, the owning side corresponds to the side that
contains the corresponding foreign key.
For many-to-many bidirectional relationships either side may be the owning side.
Unidirectional Relationships
In a unidirectional relationship, only one entity has a relationship field or property that refers to
the other. For example, LineItem would have a relationship field that identifies Product, but
Product
would not have a relationship field or property for LineItem. In other words, LineItem
knows about Product, but Product doesn't know which LineItem instances refer to it.
Queries and Relationship Direction
Java Persistence query language queries often navigate across relationships. The direction of a
relationship determines whether a query can navigate from one entity to another. For example,
a query can navigate from LineItem to Product but cannot navigate in the opposite direction.
For Order and LineItem, a query could navigate in both directions, because these two entities
have a bidirectional relationship.
Cascade Deletes and Relationships
Entities that use relationships often have dependencies on the existence of the other entity in
the relationship. For example, a line item is part of an order, and if the order is deleted, then the
line item should also be deleted. This is called a cascade delete relationship.
Cascade delete relationships are specified using the cascade=REMOVE element specification for
@OneToOne
and @OneToMany relationships. For example:
@OneToMany(cascade=REMOVE, mappedBy=
"customer")
public Set<Order> getOrders() { return orders; }
Entity Inheritance
Entities support class inheritance, polymorphic associations, and polymorphic queries. They
can extend non-entity classes, and non-entity classes can extend entity classes. Entity classes
can be both abstract and concrete.
The roster example application demonstrates entity inheritance, and is described in
"Entity
Inheritance in the roster Application" on page 727
.
Abstract Entities
An abstract class may be declared an entity by decorating the class with @Entity. Abstract
entities differ from concrete entities only in that they cannot be instantiated.
Entities
Chapter 24 · Introduction to the Java Persistence API
691