收到"query did not return a unique result"错误。我该如何解决它?



我有一个数据库,里面有各种表和东西,我使用了一个相当大的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);
}

我用wicket做了一个测试网站,并把它放在一个按钮后面。这是按钮的类别:

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 Data期望的是单个结果,而不是155。

我认为问题是formatReportData的签名没有返回集合。如果ResultSet是一个实体或DTO,它应该是:

@Query(...)
List<ResultSet> formatReportData(...);

或者,您可以使用更通用的:

@Query(...)
List<Object[]> formatReportData(...);

最新更新