035. JPA Entity Relationships - dkkahm/study-springfamework5 GitHub Wiki

Type of Relationships

  • @OneToOne
  • @OneToMany
  • @ManyToOne
  • @ManyToMany

Unidirectional and Bidirectional

Owning Side

  • Hold the foreign key
  • OneToOne
  • OneToMany and ManyToOne is the 'Many' side
  • mappedBy is used to defined the field with "own" the reference of the relationshop

Fetch Type

  • Lazy or Eager
  • JPA 2.1 Fetch Type Defaults
    • OneToMany - Lazy
    • ManyToOne - Eager
    • ManyToMany - Lazy
    • OneToOne - Eager

JPA Cascade Type

  • PERSIST
    • Save operations will cascades to related entites
  • MERGE
    • related entities are merged when the owning entity is merged
  • REFRESH
    • related entities are refreshed when the owning entity is refreshed
  • REMOVE
    • Removes all related entites when the owining entity is deleted
  • DETACH
    • detaches all related entites if a manual detach occurs
  • ALL
    • Applies all the above options
  • By defaults, no operations are cascaded

Embeddable Types

Inheritance

  • MappedSuperclass
    • Entities inherit from a super class
    • A database table IS NOT created for the super class
    • Annotation for super class
  • Single Table
    • Hibernate Default
    • One Table is used for all subclasses
  • Joined Table
    • Base class and subclasses have their own table
    • Fetching subclass entities require a join to the parent table
  • Table Per Class
    • Each subclass has its own table

Create and Update Timestamps

  • @PrePersist and @PerUpdate
    • JPA supports
  • @CreationTimeStamp and @UpdateTimestamp
    • Hiberate provides