@Query Annotation — Concept Check
Questions on @Query Annotation. @Query overrides the derived query with explicit JPQL or native SQL; use @Modifying with @Transactional for bulk UPDATE or DELETE repository methods.
Questions in this quiz
8 questions on @Query Annotation — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer uses @Query with a JPQL statement but omits @Modifying for a bulk UPDATE. What happens when the method is executed?
- AAn exception is thrown by Spring Data.
- BThe method compiles but fails at runtime.
- CThe update is executed successfully.
- DThe update is ignored silently.
2.In a Spring Data repository, a method is annotated with @Modifying and @Transactional but lacks clearAutomatically=true. What issue might arise?
- AStale entities in the first-level cache.
- BThe database will be locked.
- CThe query will not execute.
- DThe transaction will not commit.
3.A method uses @Query with a native SQL statement for a complex query. What is a potential downside of this approach?
- AIncreased readability of the query.
- BLoss of portability across different databases.
- CAutomatic transaction handling.
- DImproved performance over JPQL.
4.A developer uses @Query with a JPQL constructor expression. What is the main advantage of this approach?
- AImproved query performance.
- BDirect mapping to DTOs without manual conversion.
- CAutomatic transaction management.
- DSimplified query syntax.
5.When using @Modifying with clearAutomatically=true, what benefit does it provide after a bulk update?
- AIt prevents deadlocks in the database.
- BIt improves query execution speed.
- CIt clears the first-level cache, preventing stale data issues.
- DIt automatically commits the transaction.
6.A developer mistakenly believes that @Transactional is optional for bulk DELETE operations. What will happen if it's omitted?
- ASpring Data will automatically add a transaction.
- BThe database will lock the table.
- CThe DELETE operation will not execute.
- DThe operation will execute outside a transaction, risking data inconsistency.
7.What happens if flushAutomatically=true is set in @Modifying for a bulk update?
- AThe query execution is delayed.
- BThe update is ignored if there are no pending changes.
- CPending entity changes are flushed before the update.
- DThe update runs in a separate transaction.
8.A developer uses positional parameters in a JPQL @Query. What is a potential drawback of this approach?
- AReduced readability compared to named parameters.
- BIncompatibility with native SQL.
- CAutomatic transaction handling is disabled.
- DIncreased execution time.