简体   繁体   English

在 jpa 中使用 @query 选择第一条记录

[英]select the first record using @query in jpa

I have a jpa query that holds two records, from the two records I am unable to select the first record using @query in jpql我有一个保存两条记录的 jpa 查询,从两条记录中我无法在 jpql 中使用 @query 选择第一条记录

Here is my snippet这是我的片段

@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);

From findings I am trying to use the LIMIT 1 but no success根据调查结果,我正在尝试使用 LIMIT 1 但没有成功

Your query returns a single History entity.您的查询返回单个History实体。 If you plan to return more than one History entity you need to return a collection eg a list:如果您计划返回多个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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM