10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Entities<br />

688<br />

One-to-one: Each entity instance is related to a single instance of another entity. For example, to<br />

model a physical warehouse in which each storage bin contains a single widget, StorageBin and<br />

Widget would have a one-to-one relationship. One-to-one relationships use the<br />

javax.persistence.OneToOne annotation on the corresponding persistent property or field.<br />

One-to-many: An entity instance can be related to multiple instances of the other entities. A<br />

sales order, for example, can have multiple line items. In the order application, Order would<br />

have a one-to-many relationship with LineItem. One-to-many relationships use the<br />

javax.persistence.OneToMany annotation on the corresponding persistent property or field.<br />

Many-to-one: Multiple instances of an entity can be related to a single instance of the other<br />

entity. This multiplicity is the opposite of a one-to-many relationship. In the example just<br />

mentioned, from the perspective of LineItem the relationship to Order is many-to-one.<br />

Many-to-one relationships use the javax.persistence.ManyToOne annotation on the<br />

corresponding persistent property or field.<br />

Many-to-many: <strong>The</strong> entity instances can be related to multiple instances of each other. For<br />

example, in college each course has many students, and every student may take several courses.<br />

<strong>The</strong>refore, in an enrollment application, Course and Student would have a many-to-many<br />

relationship. Many-to-many relationships use the javax.persistence.ManyToMany<br />

annotation on the corresponding persistent property or field.<br />

Direction in Entity Relationships<br />

<strong>The</strong> direction of a relationship can be either bidirectional or unidirectional. A bidirectional<br />

relationship has both an owning side and an inverse side. A unidirectional relationship has only<br />

an owning side. <strong>The</strong> owning side of a relationship determines how the Persistence runtime<br />

makes updates to the relationship in the database.<br />

Bidirectional Relationships<br />

In a bidirectional relationship, each entity has a relationship field or property that refers to the<br />

other entity. Through the relationship field or property, an entity class’s code can access its<br />

related object. If an entity has a related field, then the entity is said to “know” about its related<br />

object. For example, if Order knows what LineItem instances it has and if LineItem knows<br />

what Order it belongs to, then they have a bidirectional relationship.<br />

Bidirectional relationships must follow these rules:<br />

■ <strong>The</strong> inverse side of a bidirectional relationship must refer to its owning side by using the<br />

mappedBy element of the @OneToOne, @OneToMany,or@ManyToMany annotation. <strong>The</strong><br />

mappedBy element designates the property or field in the entity that is the owner of the<br />

relationship.<br />

■ <strong>The</strong> many side of many-to-one bidirectional relationships must not define the mappedBy<br />

element. <strong>The</strong> many side is always the owning side of the relationship.<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!