[英]How to avoid this error on Spring Boot: (query did not return a unique result: 4)
[英]Getting an "query did not return a unique result" error. How can I fix it?
我有一个包含各种表和东西的数据库,我使用了一个相当大的 SQL 查询,该查询从所有表中收集值并对其进行排序,以便我可以轻松地将结果导出为 CSV。
我通过heidisql创建了一个存储过程,通过Java触发查询。 为此,我决定使用表的存储库,该存储库仅存储最后一个单击按钮的用户的名称 + 该操作的时间戳,因为我想避免创建新实体。
当我手动执行查询时,我得到了 155 行的正确结果。 但是当我点击按钮时,它会抛出一个错误:
Caused by: javax.persistence.NonUniqueResultException: query did not return a unique result: 155
155没问题。 我做错了什么? 我必须改变什么才能让它发挥作用?
这是它的一个小概述:
@Repository
public interface LogReportRepository extends JpaRepository<LogReport, Long>, Serializable {
@Query(value = "CALL FORMAT_REPORT_DATA(:vonMonatParam,:bisMonatParam,:aggregationThresholdParam,:teamParam);", nativeQuery = true)
ResultSet formatReportData(@Param("vonMonatParam") LocalDateTime fromMonth,
@Param("bisMonatParam") LocalDateTime untilMonth,
@Param("aggregationThresholdParam") int aggregationThreshold,
@Param("teamParam") int team);
}
@Service
public class LogReportImpl implements LogReportService {
@Autowired
LogReportRepository logReportRepository;
@Override
public ResultSet formatReportData(LocalDateTime fromMonth, LocalDateTime untilMonth, int aggregationThreshold, int team) {
return logReportRepository.formatReportData(fromMonth,untilMonth,aggregationThreshold,team);
}
}
public interface LogReportService {
ResultSet formatReportData(LocalDateTime fromMonth, LocalDateTime untilMonth, int aggregationThreshold, int team);
}
我用检票口制作了一个测试网站并将其放在按钮后面。 这是按钮的 class:
public class FormatButton extends Button {
@SpringBean
public LogReportService logReportService;
public FormatButton(String id, IModel<String> model) {
super(id, model);
}
@Override
public void onSubmit() {
LocalDateTime fromMonth = LocalDateTime.of(2022,6,30,23,59,0);
LocalDateTime untilMonth = LocalDateTime.of(2022,8,1,0,0,0);
int aggregationThreshold = 37;
int team = 1;
ResultSet resultSet = logReportService.formatReportData(fromMonth,untilMonth,aggregationThreshold,team);
}
}
该错误告诉您 Spring 数据需要一个结果而不是 155。
我认为问题在于formatReportData
的签名不返回集合。 如果ResultSet
是一个实体或 DTO,它应该是:
@Query(...)
List<ResultSet> formatReportData(...);
或者,您可以使用更通用的:
@Query(...)
List<Object[]> formatReportData(...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.