native@query返回map-i get javax.persistence.ununiqueresultexception

yyhrrdl8  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(243)

我有一个关于在我的jparepository上使用原生@query注解的问题。现在,我已经自动连接了存储库,使用我自己的接口扩展了jparepository。我试图查询一个数据库中的条目数,并试图确保所有繁重的工作都是在数据库一侧完成的。

public interface PersonRepository extends JpaRepository<Person, Long>{

    @Query(value="SELECT protocol, COUNT(Distinct personID) from person_counts WHERE(datetime BETWEEN :beginDate and :endDate)and personID NOT LIKE '%test%' GROUP by protocol", nativeQuery=true)
    Map<String, BigInteger> findPersonCountsByDate(Date beginDate, Date endDate);

}

我就是这么想的。我确保sql查询在数据库中工作。
正如你所看到的,我只想统计一下人数,按礼节分类。不过,我经常遇到例外情况:

Servlet.service() for servlet [dispatcherServlet] in context with path [/api] threw exception [Request processing failed; nested exception is org.springframework.dao.IncorrectResultSizeDataAccessException: query did not return a unique result: 2; nested exception is javax.persistence.NonUniqueResultException: query did not return a unique result: 2] with root cause

javax.persistence.NonUniqueResultException: query did not return a unique result: 2
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:128) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]

关于我做错了什么有什么线索吗?非常感谢!

mrphzbgm

mrphzbgm1#

啊哈,我知道我可能错过了一些明显的东西!多亏了m。戴纳姆为我指出了正确的答案:
您需要返回一个list<map<string,biginteger>>才能使其正常工作。使用map<string,biginteger>它将Map一行,其中字符串是列名,biginteger是结果。我不确定制作一个列表是否真的有效(你可能需要一个列表来代替)。

相关问题