如何解决如何定义本机查询返回类型?
为了计算添加到数据库表名称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对象,应将其包含在返回的列表中:
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]
似乎我没有正确定义dto CountByDay
?如何正确定义dto以返回CountByDay
对象的列表?
解决方法
我建议您尝试这样使用构造函数表达式JPQL:
SELECT new CountByDay(count(dateadded),dateadded) from url_store WHERE dateadded >= ? and dateadded <= ? group by dateadded
,
我的工作依据是:
1。重新定义CountByDay
import java.util.Date;
public interface CountByDay {
Date getDateAdded();
int getCnt();
}
2。更新查询
@Query(value="SELECT count(dateadded) as cnt,dateadded as dateadded from url_store WHERE dateadded >= ? and dateadded <= ? group by dateadded",nativeQuery=true)
List<CountByDay> getAddedCountByDay(Date fromDate,Date toDate);
这有助于:No converter found capable of converting from type to type
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。