0
0
Spring Bootframework~5 mins

Native SQL queries in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a native SQL query in Spring Boot?
A native SQL query is a SQL statement written directly in the database's SQL language, executed through Spring Boot without translation to JPQL or HQL. It allows direct control over the SQL sent to the database.
Click to reveal answer
beginner
How do you define a native SQL query in a Spring Data JPA repository?
Use the @Query annotation with the attribute nativeQuery = true. For example: @Query(value = "SELECT * FROM users", nativeQuery = true) List<User> findAllUsers();
Click to reveal answer
intermediate
Why might you choose a native SQL query over JPQL in Spring Boot?
You might choose native SQL for complex queries not supported by JPQL, to use database-specific features, or for performance optimizations.
Click to reveal answer
intermediate
What is a key risk when using native SQL queries?
Native SQL queries can be database-specific, reducing portability. They also increase risk of SQL injection if parameters are not handled safely.
Click to reveal answer
beginner
How do you safely pass parameters to a native SQL query in Spring Boot?
Use named or positional parameters with @Param annotation and let Spring handle binding. Avoid string concatenation to prevent SQL injection.
Click to reveal answer
Which annotation attribute enables native SQL in Spring Data JPA?
AsqlNative = true
BnativeQuery = true
Cnative = true
DqueryType = native
What is a benefit of using native SQL queries?
AThey automatically prevent SQL injection
BThey are always database independent
CThey allow use of database-specific features
DThey replace the need for repositories
How should parameters be passed to native SQL queries to avoid SQL injection?
AUse @Param annotation with named parameters
BConcatenate strings manually
CUse raw string interpolation
DPass parameters as plain text in the query
What happens if you omit nativeQuery = true in @Query with SQL syntax?
AThe query runs as native SQL anyway
BThe query runs twice
CSpring ignores the query
DSpring treats it as JPQL and may cause errors
Which of these is a risk of using native SQL queries?
AThey reduce database portability
BThey cannot return entity objects
CThey automatically sanitize inputs
DThey always run slower than JPQL
Explain how to create and use a native SQL query in a Spring Boot repository.
Think about the annotation and parameter binding.
You got /5 concepts.
    Describe the advantages and risks of using native SQL queries in Spring Boot.
    Consider both benefits and security concerns.
    You got /2 concepts.