Database Relationships in Spring
Learn about a few interview questions regarding database relationships in Spring.
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
annotations can be used to change the default values.
It should be noted that these annotations can be used on the owning side of the relationship only. Using them on the inverse side will result in an error.
What is the mappedBy
attribute?
The mappedBy
attribute is used on the inverse side of a relationship. This attribute specifies the name of the property in the owner side of the relationship.
If the mappedBy
attribute is missing, JPA treats the bidirectional relationship as two unidirectional relationships.
What is lazy loading?
Lazy loading means that data is only loaded when it is needed. When we fetch the Tournament
entity, the associated Registrations
are not loaded. If the EntityManager
is closed or id before the inverse siddef the entity is detached, then we will not be able to get the registrations.
What happens if the owning side of the relationship is saved before the inverse side?
The owning side (parent) keeps a reference (foreign key) of the inverse side (child). When the owning entity persists before the inverse entity, Hibernate can automatically reorder the SQL statements to avoid relationship dependency problems. Some JPA providers do not resolve relationship dependencies and throw an exception or a foreign key violation constraint. Additional configuration is needed in such a case.
Why should CascadeType.REMOVE
be used after due consideration?
In one-to-many or many-to-many relationships, CascadeType.REMOVE
should be used with care. Reason being, it removes more records than necessary. For example, deleting a Professor
entity deletes all the Course
taught by the professor.
Same goes for CascadeType.ALL
. In to-many relationships all the cascade types should be explicitly mentioned instead of using CascadeType.ALL
.
What is the difference between CascadeType.REMOVE
and orphanRemoval
attribute?
Both CascadeType.REMOVE
and orphanRemoval
attribute are used to remove records from database. When the association between the parent and child entities is removed, the child entity becomes an orphan. Using CascadeType.REMOVE
does not remove the orphaned entity. orphanRemoval
attribute has a more aggressive approach and removes the orphaned record as soon as the association between the parent and child entity is removed.
Get hands-on with 1400+ tech skills courses.