FetchType — EAGER vs LAZY — Concept Check
Questions on FetchType — EAGER vs LAZY. LAZY loads related entities on first access (default for collections); EAGER loads immediately with a JOIN (default for @ManyToOne, @OneToOne); always prefer LAZY to avoid unintended queries.
Questions in this quiz
8 questions on FetchType — EAGER vs LAZY — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer notices that accessing an order's customer name causes a delay. The order has a @ManyToOne association with Customer using FetchType.LAZY. What is likely happening?
- AThe delay is due to a separate SELECT query for the customer.
- BThe delay is because the customer is loaded eagerly with the order.
- CThe delay is due to a LazyInitializationException.
- DThe delay is caused by an N+1 query problem.
2.An application is experiencing performance issues due to excessive database queries. The Order entity has a @OneToMany association with OrderItem using FetchType.EAGER. What could be the cause?
- AThe EAGER fetch causes an N+1 query problem.
- BThe LAZY fetch is delaying query execution.
- CThe EAGER fetch is loading all orders at once.
- DThe LAZY fetch is causing too many queries.
3.A developer receives a LazyInitializationException when accessing an order's customer outside a transaction. Which fix is appropriate?
- AIgnore the exception as it is harmless.
- BChange FetchType to EAGER for the customer.
- CUse JOIN FETCH within the transaction.
- DAccess the customer within a closed session.
4.A developer mistakenly believes that using FetchType.EAGER will always improve performance. Why is this incorrect?
- AEAGER fetch prevents any database queries.
- BEAGER fetch always causes LazyInitializationException.
- CEAGER fetch can lead to unnecessary data loading.
- DEAGER fetch is never supported by Hibernate.
5.Which FetchType should be generally preferred to avoid unintended queries and improve performance?
- AEAGER, to avoid LazyInitializationException.
- BLAZY, to preload all data immediately.
- CEAGER, to ensure data is always available.
- DLAZY, to control when data is loaded.
6.A developer uses FetchType.EAGER for a @ManyToOne association. When is this acceptable?
- AWhen the associated data is small and frequently accessed.
- BWhen the associated data is always accessed in a separate transaction.
- CWhen the associated data is large and rarely accessed.
- DWhen the associated data is never accessed.
7.After changing a @OneToMany association from EAGER to LAZY, a developer notices fewer queries. Why?
- AEAGER fetch defers loading until accessed, reducing queries.
- BLAZY fetch defers loading until accessed, reducing queries.
- CEAGER fetch preloads all data, reducing queries.
- DLAZY fetch preloads all data, reducing queries.
8.What is a common mistake when fixing LazyInitializationException?
- AChanging FetchType to EAGER, causing performance issues.
- BUsing a DTO projection to avoid proxies.
- CUsing JOIN FETCH to load data within a transaction.
- DAccessing data within a @Transactional method.