Entity Relationships — Concept Check
Questions on Entity Relationships. @OneToMany, @ManyToOne, and @ManyToMany map object relationships to foreign keys and join tables. Choosing the right fetch type and understanding the owning side prevents the N+1 problem and LazyInitializationException.
Questions in this quiz
8 questions on Entity Relationships — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer notices that loading a list of 20 orders results in 21 SQL queries being executed. What is the most likely cause?
- AImproper use of bidirectional relationships
- BThe N+1 problem due to lazy loading of collections
- CUsing EAGER fetch type on collections
- DIncorrect use of cascade types
2.Which fetch type should be avoided on collections to prevent performance issues?
- AEAGER fetch type
- BJOIN FETCH
- CLAZY fetch type
- D@EntityGraph
3.An application experiences a LazyInitializationException when accessing an order's items. What could be a potential solution?
- AIncrease the database connection pool size
- BUse JOIN FETCH to load items with the order
- CSwitch to EAGER fetch type for items
- DRemove the @OneToMany relationship
4.A developer uses @ManyToMany for a relationship with extra columns in the join table. What is the recommended approach?
- AAvoid using cascade types altogether
- BDirectly use @ManyToMany with cascade REMOVE
- CUse an explicit join entity instead of @ManyToMany
- DUse EAGER fetch type to load all related entities
5.Which of the following statements about cascade types is correct?
- ACascade MERGE only affects the owning side
- BCascade ALL is safe for any relationship
- CCascade REMOVE should not be used on non-owning sides
- DCascade PERSIST does not affect child entities
6.A developer sets FetchType.EAGER on a collection. What is a potential consequence?
- AAll related entities are loaded immediately, risking performance issues
- BBidirectional relationships become unnecessary
- CLazyInitializationException will occur
- DThe N+1 problem is automatically resolved
7.What is the owning side in a @OneToMany relationship?
- AThe @ManyToOne side holding the foreign key
- BThe @OneToMany side mapped by the owning side
- CThe side with cascade REMOVE enabled
- DThe side with FetchType.EAGER
8.Which approach can help in resolving the N+1 problem?
- AIncreasing the database index size
- BRemoving bidirectional relationships
- CSwitching all relationships to EAGER fetch type
- DUsing JOIN FETCH in JPQL queries