[英]Reading with Hibernate Criteria API
I'm using Hibernate Criteria API in my project. 我在项目中使用了Hibernate Criteria API。 In this API,
list()
method is used to get a number of records from db. 在此API中,
list()
方法用于从db获取许多记录。 My question is : How to get only one record from db (without using list()
method as there is no need)? 我的问题是:如何仅从db中获取一条记录(无需使用
list()
方法,因为不需要)?
If you're certain that the query is going to return at most one record, use Criteria#uniqueResult()
. 如果确定查询最多返回一条记录,请使用
Criteria#uniqueResult()
。
If the query can return more than one record, but you're fine with just getting the first result, you could combine uniqueResult()
with Criteria#setMaxResults()
. 如果查询可以返回多个记录,但是只获取第一个结果就可以,可以将
uniqueResult()
与Criteria#setMaxResults()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.