Native SQL Queries — Concept Check
Questions on Native SQL Queries. @Query(nativeQuery=true) sends raw SQL to the DB; useful for DB-specific features or complex queries that JPQL cannot express; results can map to entities or projections.
Questions in this quiz
8 questions on Native SQL Queries — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have a Spring Data JPA repository method using a native query without a countQuery attribute. What happens when you try to paginate the results?
- AThe query executes but ignores pagination.
- BThe query executes but returns incorrect results.
- CAn exception is thrown due to missing countQuery.
- DPagination works fine without any issues.
2.A developer uses @Query(nativeQuery=true) to execute a raw SQL query. They concatenate user input directly into the SQL string. What risk does this pose?
- AAutomatic SQL optimization.
- BEnhanced readability.
- CSQL injection attacks.
- DImproved performance.
3.Which feature can you utilize with native queries that JPQL does not support?
- AEntity relationships
- BWindow functions
- CBasic CRUD operations
- DNamed parameters
4.Why must you use named or positional bind parameters in native queries?
- ATo improve query execution speed.
- BTo prevent SQL injection attacks.
- CTo simplify query syntax.
- DTo automatically map results to entities.
5.A developer uses a native query with a projection interface. What is the purpose of this approach?
- ATo select a subset of columns.
- BTo execute a CTE.
- CTo enable automatic pagination.
- DTo perform a full-text search.
6.In a native query, what must you do to map results to a DTO using a constructor expression?
- AEnsure the query selects columns matching the DTO constructor.
- BConcatenate user input directly into the query.
- CUse a countQuery attribute.
- DAvoid using bind parameters.
7.A service using native queries with window functions suddenly slows down. What could be the cause?
- AInefficient SQL execution plan.
- BMissing countQuery attribute.
- CIncorrect use of bind parameters.
- DIncorrect mapping to entity class.
8.What is a common mistake when using native queries for pagination in Spring Data JPA?
- AOmitting the countQuery attribute.
- BUsing bind parameters.
- CUsing projection interfaces.
- DSelecting specific columns.