Spring: InvalidDataAccessApiUsageException?

Spring: InvalidDataAccessApiUsageException?

我遇到以下异常:

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)]

这是我存储库中的查询方法:

 @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);

我在组件中的实现:

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

为什么会出现此错误?看来实现中的两个 DateTime 参数与我方法中的匹配?

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

不要在方法参数中使用 joda 的 DateTime,而是使用 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);

然后在您的客户端代码中,如果您有一些 DateTime 实例,您可以使用 toDate 方法将 DateTime 转换为 Date:

personRepo.getPersonBetweenDates(new DateTime().withTimeAtStartOfDay().toDate(), DateTime.now().toDate());