Java 春天:InvalidDataAccessApiUsageException?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35577738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring: InvalidDataAccessApiUsageException?
提问by java123999
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 DateTimeparameters in the implementation match those in my method?
为什么我收到这个错误?实现中的两个DateTime参数似乎与我的方法中的参数相匹配?
采纳答案by Ali Dehghani
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:
不要DateTime
在方法参数中使用 joda,而是使用java.util.Date
, 如下所示:
@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
:
然后在您的客户端代码中,如果您有一些DateTime
实例,则可以使用toDate
方法将其DateTime
转换为Date
:
personRepo.getPersonBetweenDates(new DateTime().withTimeAtStartOfDay().toDate(), DateTime.now().toDate());