0
0
Spring Bootframework~5 mins

@Query for custom JPQL in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @Query annotation in Spring Data JPA?
The @Query annotation lets you write custom JPQL or SQL queries directly in your repository methods to fetch data in ways not covered by method names.
Click to reveal answer
beginner
How do you write a simple JPQL query using @Query to find users by their email?
You write @Query("SELECT u FROM User u WHERE u.email = :email") above the repository method, and use @Param("email") to bind the method parameter.
Click to reveal answer
intermediate
Can @Query use native SQL queries instead of JPQL? How?
Yes, by adding nativeQuery = true in the @Query annotation, you can write native SQL queries instead of JPQL.
Click to reveal answer
intermediate
What happens if you omit the @Param annotation in a @Query method with named parameters?
Spring Data JPA may not bind the method parameters correctly to the query parameters, causing errors or unexpected results.
Click to reveal answer
intermediate
How does @Query improve flexibility compared to derived query methods in Spring Data JPA?
It allows writing complex queries with joins, aggregations, or conditions that are hard or impossible to express with method names alone.
Click to reveal answer
What does the @Query annotation in Spring Data JPA allow you to do?
AWrite custom JPQL or SQL queries
BAutomatically generate database tables
CConfigure database connections
DCreate REST endpoints
How do you specify a native SQL query in @Query?
AUse @NativeQuery annotation
BAdd nativeQuery = true in the annotation
CWrite SQL without any extra flags
DSet native = true in application.properties
Which keyword is used in JPQL to refer to an entity?
ADATABASE
BTABLE
CFROM
DCOLLECTION
What annotation should you use to bind method parameters to named parameters in @Query?
A@QueryParam
B@Bind
C@Named
D@Param
If you want to write a query with a join in Spring Data JPA, which is the best approach?
AUse @Query with JPQL
BUse method name conventions only
CWrite SQL in application.properties
DUse @JoinColumn annotation
Explain how to use the @Query annotation to write a custom JPQL query with parameters in Spring Data JPA.
Think about how you write the JPQL and connect it to method inputs.
You got /4 concepts.
    Describe the difference between JPQL and native SQL queries when using @Query in Spring Data JPA.
    Consider how the query language relates to the database and entities.
    You got /4 concepts.