JpaRepository中的Native@Query返回映射—;我得到javax.persistence.ununiqueresultexception
我有一个关于在我的JpaRepository上使用原生@Query注释的问题。现在,我使用自己的接口自动连接了存储库,该接口扩展了JpaRepository。我试图查询数据库中的条目数,并试图确保所有繁重的工作都是在数据库一侧完成的JpaRepository中的Native@Query返回映射—;我得到javax.persistence.ununiqueresultexception,java,spring,jpa,jdbc,Java,Spring,Jpa,Jdbc,我有一个关于在我的JpaRepository上使用原生@Query注释的问题。现在,我使用自己的接口自动连接了存储库,该接口扩展了JpaRepository。我试图查询数据库中的条目数,并试图确保所有繁重的工作都是在数据库一侧完成的 public interface PersonRepository extends JpaRepository<Person, Long>{ @Query(value="SELECT protocol, COUNT(Disti
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);
}
关于我做错了什么有什么提示吗?非常感谢 啊哈,我知道我可能错过了一些明显的东西!感谢您为我指出了正确的答案: 您需要返回一个列表才能使其正常工作。使用映射,它将映射一行,其中字符串是列名,结果是BigInteger。我不确定制作一个列表是否真的有效(你可能需要一个列表)
在您的查询中,map的键是什么?请检查它是否在您的查询中返回了不止一次。哦,很好!是的,我在编辑变量以在这里公开发布,并且不小心切换了字符串和BigInteger。我修复了代码段,但仍然出现异常。您需要返回一个
列表
,才能使其正常工作。使用映射
它将映射一行,其中字符串
将是列名,biginger
将是结果。我不确定列表是否真的有效(您可能需要一个列表
)。
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]