【问题标题】:select the first record using @query in jpa在 jpa 中使用 @query 选择第一条记录
【发布时间】:2020-06-25 17:55:49
【问题描述】:

我有一个包含两条记录的 jpa 查询,我无法在 jpql 中使用 @query 从两条记录中选择第一条记录

这是我的sn-p

@Query("select h from History h where h.id =:id and h.status =:status " +
            "and h.type =:type and h.user =:user")
    History getAHistory(@Param("id") Long id, @Param("status") Status status,
                                         @Param("type") Type type, @Param("user") User user);

根据调查结果,我正在尝试使用 LIMIT 1 但没有成功

【问题讨论】:

标签: java jpa spring-data-jpa jpql


【解决方案1】:

您的查询返回单个 History 实体。如果您计划返回多个 History 实体,则需要返回一个集合,例如一个列表:

@Query("select h from History h where h.id = :id and h.status = :status " +
       "and h.type = :type and h.user = :user")
List<History> getAHistory(@Param("id") Long id, @Param("status") Status status,
                          @Param("type") Type type, @Param("user") User user);

【讨论】:

  • 所以列表是解决这个问题的唯一方法
  • 这是一种方法,或者使用只返回一行的查询和参数。与id 参数过滤返回多行有关的是什么。这不是PK,所以很难说它是什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-28
  • 2013-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多