Java 在 JPA 中获取单行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44173586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:23:43  来源:igfitidea点击:

Get single row in JPA

javaspring-data-jpajpql

提问by Akkave

How to get single row from entity in JPA?

如何从JPA中的实体获取单行?

Table: Employee

表:员工

    @Id
    private int empId;
    private String empName;
    ...    

JPA by default return List. I`m trying to fetch single row.

JPA 默认返回列表。我正在尝试获取单行。

EmployeeRepository :-

员工资料库:-

    public Employee findByEmpName(String empName);

Another way is to do it, @Query should be use.

另一种方法是这样做,应该使用@Query。

    @Query(value="select e from Employee e where empName = ?1 limit 1", nativeQuery=true)
    public Employee findByEmpName(String empName);

How can i ensure that it return single row and correct result. Any help appreciated.. Thanks in advance.

我如何确保它返回单行和正确的结果。任何帮助表示赞赏.. 提前致谢。

采纳答案by Zeromus

You don't need to "ensure" anything.

您不需要“确保”任何事情。

If you dont have a collection of sort specified as return (e.g. List<Employee>instead of Employee) and your query return more than one result it will launch a javax.persistence.NonUniqueResultException: result returns more than one elements.

如果您没有将排序集合指定为 return(例如List<Employee>代替Employee)并且您的查询返回多个结果,它将启动一个 javax.persistence.NonUniqueResultException: result returns more than one elements.

If your query return more rows and you want only one of those either add a condition to differentiate which one of those rows you actually want or, if they are the same, add a good old distinct

如果您的查询返回更多行,而您只想要其中一个行,要么添加一个条件来区分您实际想要的行中的哪一行,或者,如果它们相同,则添加一个旧的 distinct

How can i ensure that it return single row and correct result.

我如何确保它返回单行和正确的结果。

That's your job when writing the query

这是您编写查询时的工作

回答by Lokesh Nandanwar

If you want to select only one row from the result set then you can limit number of records by using the query.setMaxResultsmethod:while creating the jpa query.

如果您只想从结果集中选择一行,那么您可以使用query.setMaxResults方法限制记录数:同时创建 jpa 查询。

example : criteria.setMaxResults(25); : it only fetch 25 records out of 100 records.

示例:criteria.setMaxResults(25); :它只提取 100 条记录中的 25 条记录。

回答by fireandfuel

JPA have a method to fetch a single row getSingleResult, but it's recommended to fetch a list and extract the first element over getResultList.

JPA 有一种获取单行的方法getSingleResult,但建议获取列表并提取第一个元素getResultList

See: Example

请参阅: 示例

回答by Igor

In case someone wants just a single row from a database using JpaRepository (spring-data-jpa), I found this very useful:

如果有人只需要使用 JpaRepository (spring-data-jpa) 的数据库中的一行,我发现这非常有用:

repository.findAll().stream().findFirst().orElseThrow(...)

Since this is a stream, I suppose that rows (or rather: Objects) are fetched row by row.

由于这是一个流,我认为行(或者更确切地说:对象)是逐行获取的。

回答by milosdju

There is an out of the box solution, in repository you can name method as findFirstBy...or findTopBy..., that should do the thing.

有一个开箱即用的解决方案,在存储库中,您可以将方法命名为findFirstBy...or findTopBy...,这应该可以解决问题。

You can find more in Spring reference documentation

您可以在Spring 参考文档中找到更多信息