Java 我们可以从 hql 查询中获取单个对象吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27901795/
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
Can we get single object from hql query?
提问by Raghu
I am writing a select query in hql , my task is to activate the bus. First I will get a messege from client as busId#busStatus
, so first I look for this perticular busId is active or inactive So I have to write select query but in hibernate query.list()
returns list
. Here I think list is unnecessary , a single object is enough .
我正在 hql 中编写一个选择查询,我的任务是激活总线。首先,我将从客户端获取消息busId#busStatus
,因此首先我会查找此特定 busId 是活动的还是非活动的,因此我必须编写选择查询,但在休眠状态下query.list()
返回list
。这里我认为列表是不必要的,一个对象就足够了。
Here is my code ,
这是我的代码,
String hql="from BusDetailBean where Busid= :busId and bus_status=:busStatus";
Query query = session.createQuery(hql);
query.setParameter("busId", busId);
query.setParameter("busStatus", busStatus);
List<BusDetailBean> busDetails=(List<BusDetailBean>)query.list();
if(busDetails.isEmpty())
{
//my other stuff
}
else
{
//bus ativation stuff
}
My question is the select query returns only one object if list is not empty I have to use for loop in else part. So how can I optimise this code. can anyone help me in this.
我的问题是如果列表不为空,选择查询只返回一个对象我必须在其他部分使用 for 循环。那么我该如何优化这段代码。任何人都可以帮助我。
回答by Jens
You can get the object at index 0 in the list:
您可以在列表中的索引 0 处获取对象:
List l = query.list()
if (l.size()>0) {
return l.get(0)
}
回答by Kalaiarasan Manimaran
You can use query.setMaxResults(1);
您可以使用 query.setMaxResults(1);
回答by CodZilla
You can use query.getSingleResult()
您可以使用 query.getSingleResult()
回答by slnowak
I don't think persitence should be mixed with business logic.
我不认为持久性应该与业务逻辑混合在一起。
What about returning Optional from the persitence layer and whether result is present/absent do something in higher level?
从持久层返回 Optional 以及结果是否存在/不存在如何在更高级别上做一些事情?
In persistance layer something like:
在持久层类似:
return query.list()
.stream()
.findFirst()
return query.list()
.stream()
.findFirst()
This, according to docs, will return first result or empty optional if the collection was empty.
根据文档,如果集合为空,这将返回第一个结果或空可选。
And then:
进而:
Optional<Bus> optionalBus = repository.find(busId, busStatus);
if (optionalBus.isPresent()) {
something here
} else {
something else
}
回答by alexd
by using query.uniqueResult() you don't ensure that if you have many results , then you will get only one of them.
通过使用 query.uniqueResult() 你不能确保如果你有很多结果,那么你只会得到其中一个。
With uniqueResult() you place a guard/contraint at your result set to be aware that this query should always return a unique result.
使用 uniqueResult() 您可以在结果集中放置一个保护/约束,以了解此查询应始终返回唯一结果。
回答by avs
For this type of problem, the out of the box solution in Hibernate
is to use the uniqueResult()
method in the Query
class:
对于这类问题,开箱即用的解决方案Hibernate
是使用类中的uniqueResult()
方法Query
:
public Object uniqueResult()
From the Hibernate
JavaDocs:
来自Hibernate
JavaDocs:
Convenience method to return a single instance that matches the query, or
null
if the query returns no results.Returns: the single result or
null
Throws:
NonUniqueResultException
- if there is more than one matching resultHibernateException
返回与查询匹配的单个实例的便捷方法,或者
null
如果查询没有返回结果。返回: 单个结果或
null
抛出:
NonUniqueResultException
- 如果有多个匹配结果HibernateException