5

This example works fine.

@Query("select t from TimeTable t where MONTH(t.date) = ?1 and YEAR(t.date) = ?2")
List<TimeTable> findAll(Integer month, Integer year);

Now I try to replace ?1 and ?2 with names like below

@Query("select t from TimeTable t where MONTH(t.date) =month and YEAR(t.date) =:year")
List<TimeTable> findAll(Integer month, Integer year);

This one doesn't work and produce error:

org.springframework.dao.InvalidDataAccessApiUsageException: Parameter with that position [1] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that position [1] did not exist

And this one

@Query("select t from TimeTable t where MONTH(t.date) =:month and YEAR(t.date) =:year")
List<TimeTable> findAll(Integer month, Integer year);

produce error:

org.springframework.dao.InvalidDataAccessApiUsageException: Name for parameter binding must not be null or empty! On JDKs < 8, you need to use @Param for named parameters, on JDK 8 or better, be sure to compile with -parameters.; nested exception is java.lang.IllegalArgumentException: Name for parameter binding must not be null or empty! On JDKs < 8, you need to use @Param for named parameters, on JDK 8 or better, be sure to compile with -parameters.

UPDATE:

like an error above says I do not need to use @Param on JDK 8, but solution with @Param works:

List<TimeTable> findAll(@Param("month") Integer month, @Param("year") Integer year);

and when I delete @Params it goes this error again.

2
  • YEAR(t.date) =:ear") misspelling?
    – krezus
    Jan 30, 2018 at 6:40
  • 1
    good eye krezus (I made misspeling just here in post, in code everything is fine)
    – degath
    Jan 30, 2018 at 6:41

3 Answers 3

11

Named Parameters using @Param

Spring Data query parameters are replaced based on their positions. But this might go wrong and binding could be error prone. Hence it is suggested to use @Param annotation in the method parameter to bind the query parameter names. And in the query, you need to use :paramName to indicate that the same paramName is to bind with the method parameter.

Update your method as below

@Query("select t from TimeTable t where MONTH(t.date) =:month and YEAR(t.date) =:year")
List<TimeTable> findAll(@Param("month") Integer month, @Param("year") Integer year);

Simply its not possible to retain method parameter names for interface

When a java class is compiled, by default java compiler changes the method parameter names. Say if you compile this code

public class Foo {
  public void bar(int myHolyParam) {}
}

You might end up having this

public class Foo {
  public void bar(int arg0) {}
}

Your parameter names are lost. You may retain the names by setting a compiler flag like -g:vars but this will only help for your Classes. Interface method parameter names can not be retained. There was no legit way to help this case before JDK 8 strikes. You can see this SO Question on this regard.

on JDK 8 or better, be sure to compile with -parameters

Lately JDK 8 comes up with the solution. If you want to retain method parameter names (for Classes or Interfaces) you can simply tell compiler by setting -parameter flag. Exploiting this JDK 8 feature, Spring can deduce the parameter names using reflection. But remember, still you need to use the compiler flag -parameters to have this ability.

So if you are not using that flag or using java version less than 8, you must to use @Param annotation to mark your parameters.

You can read a brief idea about Named Parameter in Java 8

2

The first exception happens because you wrote =month instead of =:month. This way you only have one bind parameter but two method arguments which doesn't match.

In the second case your byte code doesn't contain information about the names of the argument. Just do what the exception tells you to do:

Name for parameter binding must not be null or empty! On JDKs < 8, you need to use @Param for named parameters, on JDK 8 or better, be sure to compile with -parameters.

Like this:

@Query("select t from TimeTable t where MONTH(t.date) =:month and YEAR(t.date) =:year")
List<TimeTable> findAll(@Param("month") Integer month, @Param("year") Integer year);

If you are on Java 8 or above you can apply the other option given in the error message:

on JDK 8 or better, be sure to compile with -parameters

If you do this you don't need the @Param annotations.

See this question for background: Why are Spring Data repository method parameters names not available even on Java 8?

2
  • I updated my answer in the same time u answered. I use jdk8. This means I do not need to add @Param or am I wrong?
    – degath
    Jan 30, 2018 at 6:52
  • updated the answer Jan 30, 2018 at 6:59
0

please try this for last query.

 List<TimeTable> findAll(@Param("month") Integer month, @Param("year") Integer year); 

based on this error:

you need to use @Param for named parameters, on JDK 8 or better, be sure to compile with -parameters.

please check here for your update question.

2
  • I updated post in same time. Yeah it works with @Param, but I use JDK 8, so it should be removed.
    – degath
    Jan 30, 2018 at 6:49
  • check here. Your question already has answer there.
    – krezus
    Jan 30, 2018 at 6:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.