Enterprise Java Development@TOPIC@

Chapter 57. One-to-Many Relationships

57.1. One-to-Many: Uni-directional
57.2. @OneToMany Annotation
57.3. One-to-Many: Bi-directional
57.4. @ManyToOne Annotation
57.5. One-to-Many: Element Collection
57.6. @ElementCollection Annotation
57.7. Other Topics
57.8. Summary

Figure 57.1. One-to-Many Uni-directional

One-to-Many Uni-directional

Figure 57.2. One-to-Many Bi-directional

One-to-Many Bi-directional

Uni-directional

Only one side ("owner") knows of the relationship

  • Uses the @OneToMany annotation

  • Defines mapping to database

    • Uses either @JoinColumn or @JoinTable

    Note

    @JoinTable adds the foreign key to the child table and not to the owning entity class table in this uni-directional case

Bi-directional

Both classes know of the relationship

  • Many side required to be owning side and maps relation to the database

    • Uses the @ManyToOne annotation

    • Uses either @JoinColumn or @JoinTable

    • Changes here change the database

  • One side required to be inverse and names the other entity's property

    • @OneToMany(mappedBy="owning-property")

    • Changes here do *not* change database




  • Relationship formed when inverse side added to owning collection


Identical properties to @OneToOne annotation with the exception of no "optional" property





Many of the familiar properties




Many of the familiar properties