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

标签 java jpa spring-data-jpa jpql

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

这是我的片段

@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 但没有成功

最佳答案

您的查询返回单个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);

关于java - 在jpa中使用@query选择第一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60675165/

相关文章:

java - JList - locationToIndex() 始终返回线程中鼠标位置的相同索引

java - 如何使用某些与 SQL 兼容的列类型在 Spring JPA 中存储 Joda 时间(任何)?

eclipse - Eclipse 中的 JPA 项目问题 - 注释为@Entity : Table "xxx" cannot be resolved 的类中的错误

java - 从 jar 共享库访问资源

java - 正确使用泛型

java - 运行 Spring Boot Web 应用程序时 JVM 崩溃

java - Hibernate JPQL 抛出无法输入更新/删除查询

database - 使用 postgresql 中已存在的表创建 ER 图或任何 DB 图

java - 带有 JPA 存储库的 Spring Boot 静态 where 子句

spring-data-jpa - 为什么jpa绑定(bind)枚举参数为varbinary