Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: InvalidDataAccessApiUsageException?

I am getting the following exception:

org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value did not match expected type. [java.util.Date (n/a)]; 
nested exception is java.lang.IllegalArgumentException: Parameter value did not match expected type [java.util.Date (n/a)]

Here is my query method in my repository:

 @Query("SELECT COUNT(entity) FROM Person entity WHERE trunc(entity.date) BETWEEN :startTime AND :endTime")
 int getPersonBetweenDates(@Param("startTime") DateTime var1, @Param("endTime") DateTime var2);

And my implementation in component:

 int totalPersons = personRepo.getPersonBetweenDates(new DateTime().withTimeAtStartOfDay(), DateTime.now());

Why am I getting this error? It seems that the two DateTime parameters in the implementation match those in my method?

like image 288
java123999 Avatar asked Feb 23 '16 12:02

java123999


1 Answers

Parameter value did not match expected type. [java.util.Date (n/a)];

Instead of using joda's DateTime in method parameters, use java.util.Date, Like following:

@Query("SELECT COUNT(entity) FROM Person entity WHERE trunc(entity.date) BETWEEN :startTime AND :endTime")
int getPersonBetweenDates(@Param("startTime") Date var1, @Param("endTime") Date var2);

Then in your client code, if you have some DateTime instances, you can use toDate method to convert the DateTime into a Date:

personRepo.getPersonBetweenDates(new DateTime().withTimeAtStartOfDay().toDate(), DateTime.now().toDate());
like image 78
Ali Dehghani Avatar answered Oct 12 '22 13:10

Ali Dehghani