Reputation: 261
I am trying to get table data as list using select query between dateFrom and current date using MySQL Native Query in Spring Boot. and MySQL database field datatype is String.
Below is Query in Repository:
@Query(value = "select * from Account where DATETIMESTAMP >= :dateFrom AND DATETIMESTAMP < :DATE_FORMAT(curdate(), '%d/%m/%Y')", nativeQuery = true)
List<Account> findByDate(@Param("dateFrom") String dateFrom);
Getting below error for above query in Spring Boot:
Named parameter not bound : DATE_FORMAT; nested exception is org.hibernate.QueryException: Named parameter not bound : DATE_FORMAT
Can anyone please help me to frame the query for the same.
Upvotes: 6
Views: 43530
Reputation: 1
Usually when you're writing native queries, you may will break lines on your repository code. Like this:
String value = "select from People " + "where id = :id" + "and address = :address"
The error will show: Named parameter not bound :idand
Notice that the repository joined the 2 strings on sequence, and changed the parameter name and reference. You must enter an empty space in the end of each line in order to have a correct semantic
The correct way will be like:
String value = "select from People " + "where id = :id " + "and address = :address"
Upvotes: 0
Reputation: 8562
If you're using JPQL & still getting this error then its possible you might have a space in between like
where id = : myId
Should be
where id = :myId
Upvotes: 5
Reputation: 101
I had the same error, but in my case I had a comparison operator '<>' joined on to my variable with no space in between. Adding a space between the variable and the operator fixed my issue.
ie. changing :addressType<>
to :addressType <>
worked
Upvotes: 1
Reputation: 6403
In any case, If you face this problem. Before doing debugging or deep google search Check the following things
Method variable names and your query variables are matched or not.
Query semicolon with your parameters
If you renamed the table with short expressions, double-check them they were used correctly.
Check your query syntax.
Because most of the issues have come from the above minor mistakes.
Upvotes: 4
Reputation: 2764
Remove the :
from :DATE_FORMAT(curdate(), '%d/%m/%Y')
. :
is used for binding parameters in jpa query. The addition :
in front of DATE_FORMAT
makes JPA
think it as a parameter.
So the final query should be
@Query(value = "select * from Account where DATETIMESTAMP >= :dateFrom AND DATETIMESTAMP < DATE_FORMAT(curdate(), '%d/%m/%Y')", nativeQuery = true)
List<Account> findByDate(@Param("dateFrom") String dateFrom);
Upvotes: 4