Database Relationships in Spring
Explore how to define and customize database relationships in Spring using JPA annotations such as @OneToOne, @OneToMany, @ManyToOne, and @ManyToMany. Understand the use of mappedBy, lazy loading concepts, and the impacts of CascadeType.REMOVE versus orphanRemoval to manage entity lifecycle and avoid common pitfalls in relationship handling.
We'll cover the following...
We'll cover the following...
- What are the database relationship annotations in JPA?
- Name some annotations which can be used to customize database relationships.
- What is the mappedBy attribute?
- What is lazy loading?
- What happens if the owning side of the relationship is saved before the inverse side?
- Why should CascadeType.REMOVE be used after due consideration?
- What is the difference between CascadeType.REMOVE and orphanRemoval attribute?
What are the database relationship annotations in JPA?
Four annotations are used to define the underlying database structure in JPA. These are @OneToOne, @OneToMany, @ManyToOne, and @ManyToMany. Each relationship must be marked with one of the annotations.
Name some annotations which can be used to customize database relationships.
JPA provides annotations to customize the database columns, tables and foreign key values. @JoinColumn, @JoinColumns, @JoinTable, @PrimaryKeyJoinColumn and @PrimaryKeyJoinColumns ...