You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JPA查询报NonUniqueResultException,如何获取全部Player数据?

解决NonUniqueResultException获取所有Player数据的方法

问题根源在于你定义的findAllByPlayerClub方法返回类型为单个Player对象,但该查询实际会返回多条属于指定俱乐部的球员记录,JPA无法将多条结果封装为单个对象,因此抛出NonUniqueResultException。

修复方案

直接修改PlayerRepository中的查询方法,将返回类型改为List<Player>,即可接收所有匹配的球员数据:

@Repository
public interface PlayerRepository extends JpaRepository<Player, Long>{
    // 将返回类型从Player改为List<Player>
    List<Player> findAllByPlayerClub(Club club);
}

扩展用法(可选)

如果需要对结果进行分页或排序,可以进一步扩展方法:

  • 分页查询:
Page<Player> findAllByPlayerClub(Club club, Pageable pageable);
  • 指定排序规则查询:
List<Player> findAllByPlayerClubOrderByPlayerNameAsc(Club club);

内容的提问来源于stack exchange,提问作者k1313

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 10:20:39