Quick revision
The short answer
A JPQL query such as @Query("select o from Order o where o.status = :status and o.total >= :min") on a method List<Order> find(@Param("status") Status status, @Param("min") BigDecimal min) binds each method parameter to the named parameter with the same name. @Param is needed when the parameter names are not available at runtime, which is the case unless the code is compiled with the -parameters flag; Spring Boot's Maven and Gradle setups enable that flag by default, but @Param keeps the binding explicit and safe. JPQL in @Query is validated at startup, while native SQL is not.
What to remember
In this answer, we saw that @Query with named parameters binds :status and :min to the method parameters annotated @Param with the same names. Without @Param, Spring Data falls back to reflected names, which exist only when the code is compiled with -parameters, as Spring Boot's build plugins arrange by default. JPQL queries are parsed and validated at startup, while native SQL is checked only when it runs, so native queries need integration tests.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A JPQL query such as @Query("select o from Order o where o.status = :status and o.total >= :min") on a method List<Order> find(@Param("status") Status status, @Param("min") BigDecimal min) binds each method parameter to the named parameter with the same name. @Param is needed when the parameter names are not available at runtime, which is the case unless the code is compiled with the -parameters flag; Spring Boot's Maven and Gradle setups enable that flag by default, but @Param keeps the binding explicit and safe. JPQL in @Query is validated at startup, while native SQL is not.
Why this question is asked
@Query is where most Spring Data users go once a derived method name gets too long. Named parameters make those queries readable, and the @Param annotation is the part people copy without knowing why it is there, until a build without the right compiler flag fails at startup. Interviewers ask this to check that we can write a correct JPQL query, explain how Spring Data binds parameters, and know what is checked at startup and what is not. In this answer, we will recap @Query and named parameters, write the method, see how binding works, explain exactly when @Param is required, and compare startup validation for JPQL and native SQL.
1A Quick Recap of @Query and Named Parameters
@Query puts a query string on a repository method. By default the string is JPQL, the Java Persistence Query Language, which looks like SQL but talks about entities and their fields: select o from Order o refers to the Order class and its alias o, not the orders table. With nativeQuery = true it is plain SQL for the database instead.
Parameters can be positional, ?1 and ?2, bound in the order of the method parameters, or named, :status and :min, bound by name. Named parameters are easier to read and cannot be mixed up by reordering the method's parameters.
2Writing the Method
Here is the entity and the repository method. Note that Order is a reserved word in SQL, so the entity is mapped to a table called orders; JPQL can still use Order as the entity name.
Notice that :status binds to the parameter annotated @Param("status") and :min to @Param("min"). The order of the method parameters does not matter for named binding; we could swap them and the query would still be correct. The method name find no longer carries any meaning for Spring Data, because @Query takes precedence over name derivation, so we are free to choose a clear name such as findPaidAbove.
3How Binding Works and When @Param Is Needed
When the repository proxy is built at startup, Spring Data reads the query and finds the named parameters :status and :min. It then needs to know which method parameter belongs to each name. It looks for a @Param annotation on each parameter first. If there is none, it asks Java reflection for the parameter's name.
That second step is the catch. By default, the Java compiler does not keep method parameter names in the class file; reflection then reports names like arg0 and arg1. The names are kept only when the code is compiled with the javac flag -parameters. Spring Boot's parent POM for Maven and its Gradle plugin turn this flag on, so in a standard Boot project, named parameters often work without @Param. In a project built another way, or a module compiled without the flag, the same code fails.
- Parameter has @Param("min")?YesBind itNoLook up the reflected name
- Compiled with -parameters?YesName is min: bind itNoName is arg1: no match
- Fail at startupQuery names :min but no parameter is called min
3 steps in order. Play them, or step through them one at a time.
@Param is the step that never depends on the build.
So @Param is required when parameter names are not compiled in, and recommended always. It makes the binding visible in the code, survives refactoring that renames a method parameter, and does not depend on build settings that someone might change. The exact message text varies by version, so check yours, but it names both remedies.
4What Is Validated at Startup
For JPQL, Spring Data asks the JPA provider to create the query when the repository is built. Hibernate parses the string, resolves the entity name and each field, and checks the syntax. A typo such as o.totl or selct fails the application at startup with an error that names the problem, for example an UnknownPathException or a query syntax error wrapped in an IllegalArgumentException; check your Hibernate version for the exact class. That gives JPQL in @Query much of the safety of derived methods.
Native queries are different. With nativeQuery = true, the string is plain SQL, and Hibernate does not understand the database's SQL well enough to validate it. It passes the string to the database when the method is called. A wrong column name in a native query is found only at runtime, the first time that method runs.
So native queries need integration tests that actually run them against the same database engine as production, for example with Testcontainers. Named parameters with @Param work the same way in native queries.
5Follow-up questions
Should we use positional parameters instead to avoid @Param?
Positional parameters such as ?1 do not need names, but they depend on the order of the method parameters, so reordering parameters silently changes the query. Named parameters with @Param are clearer and safer, especially when two parameters have the same type.
Can a named parameter be a collection?
Yes. where o.status in :statuses with @Param("statuses") List<Status> statuses expands the list into the IN clause. Be careful with very large lists, because each element becomes a bind parameter and databases have limits.
Can I use a parameter's properties in the query?
Yes, with SpEL: :#{#filter.minTotal} reads minTotal from a parameter annotated @Param("filter"). It is handy, but it moves logic into a string, so I use it sparingly.
6Conclusion
In this answer, we saw that @Query with named parameters binds :status and :min to the method parameters annotated @Param with the same names. Without @Param, Spring Data falls back to reflected names, which exist only when the code is compiled with -parameters, as Spring Boot's build plugins arrange by default. JPQL queries are parsed and validated at startup, while native SQL is checked only when it runs, so native queries need integration tests.
Open with
I would write it as @Query with the JPQL string select o from Order o where o.status = :status and o.total >= :min, on a method like List<Order> findPaidAbove, with the parameters annotated @Param("status") Status status and @Param("min") BigDecimal min.
Then, if they want more
JPQL talks about the entity and its fields, so Order is the entity name and total is the field, even if the table is orders. At startup Spring Data finds the named parameters in the query and matches each one to a method parameter. It looks for @Param first.
If there is no @Param, it uses the parameter name from reflection, but Java only keeps parameter names in the class file when you compile with the -parameters flag.
Spring Boot's Maven parent and Gradle plugin turn that on by default, so in a standard Boot project it often works without @Param, but in a build without the flag the names come back as arg0 and arg1 and the application fails at startup.
So @Param is needed when names are not compiled in, and I use it anyway because it makes binding explicit and safe under refactoring. One more point: JPQL in @Query is parsed by Hibernate when the repository is created, so a typo in a field name fails the startup.
Native queries are passed through as SQL and only fail when they run, so I cover them with integration tests against the real database engine, for example with Testcontainers.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a @Query method with named parameters that finds orders by status and minimum...": https://scrivio-interview-library.netlify.app/databases/spring-data-jpa/58acf71923/