spring - 如何定义这个原生查询返回类型?
问题描述
为了计算添加到数据库表名的项目数,url_store
我使用带有 Spring JPA 的本机查询:
@Query(value="SELECT count(dateadded), dateadded from url_store WHERE dateadded >= ? and dateadded <= ? group by dateadded",
nativeQuery=true)
List<CountByDay> getAddedCountByDay(Date fromDate, Date toDate);
CountByDay
DTO 对象,应包含在返回的 List 中:
import java.util.Date;
public class CountByDay {
public CountByDay(Integer count, Date dateAdded){
this.count = count;
this.dateAdded = dateAdded;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
private Integer count;
public Date getDateAdded() {
return dateAdded;
}
public void setDateAdded(Date dateAdded) {
this.dateAdded = dateAdded;
}
private Date dateAdded;
}
当我调用该getAddedCountByDay
方法时,我收到错误:
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [CountByDay]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321) ~[spring-core-5.2.8.RELEASE.jar:5.2.8.RELEASE]
看来我没有CountByDay
正确定义dto?如何正确定义 dto 以返回CountByDay
对象列表?
解决方案
我建议您尝试像这样使用构造函数表达式 JPQL:
SELECT new CountByDay(count(dateadded), dateadded) from url_store WHERE dateadded >= ? and dateadded <= ? group by dateadded
推荐阅读
- regex - Google Apps 脚本中用于替换 Rs 的正则表达式
- django - 如何扩展 Django Queryset 的 select_related 函数的范围,使其包含与原始表相关的表相关的表
- doctrine - 语法错误 line 0, col 84: Error: Expected end of string, got 'f9f03b9a61c46db91ed492c862a3083'
- php - PHP Postgres Driver 9.2.18 的简单选择问题
- recursion - 查找二叉搜索树从根到特定键的距离
- java - 如何使用接口方法?
- java - 使用时间格式化日期,仅当日期有时间时
- r - 如何使用 compare_mean() 函数?
- c++ - 键入 g++ --version 在 Windows 中显示错误的版本
- java - Google Cloud Endpoints 错误原因:java.lang.IllegalArgumentException:预期的原始类,但得到了