Hibernate过滤查询集合 [英] Hibernate filtering query collections

查看:164
本文介绍了Hibernate过滤查询集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下是否可以使用hibernate来做到这一点。
假设我已经运行HQL并检索了一个集合。是否有可能使用hibernate进一步过滤它?



我试着在标题中使用< filter> 在查询之前添加session.enable(),但看起来不起作用。

示例代码

 查询搜索= session.getNamedQuery(HQL_SOMEDEFAULTQUERY); 
列表结果= search.list();
//进一步过滤...

剥离HQL

 从流中选择h 
作为f
加入f.item作为i
将i.header加入为h
where i.status =:status
and f.staff =:staff
order by i.prId desc


解决方案

没有。至少,不是你问的方式。一旦你要求Hibernate命中数据库(使用 list()方法),Hibernate已经发挥了作用,结果现在掌握在你的手中。你可以在你的代码中实现一个过滤逻辑来后处理结果。



也就是说,可以在 中过滤结果查询自己。如果你定义了一个Hibernate过滤器并为特定的模型/查询启用它,你可以保留原来的HQL查询,而Hibernate会在其中添加额外的其中子句进一步过滤结果。看到这个:



http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/filters.html


I would like to ask if it's possible to do this using hibernate. Let say I have already run a HQL and retrieved a collection. Is it possible to further filter it using hibernate?

I tried to use the <filter> to the header class and add session.enable() before the query, but seems it's not working.

Sample code

Query search = session.getNamedQuery(HQL_SOMEDEFAULTQUERY);
List results = search.list();
//further filtering ...

Stripped down HQL

select h
    from flow as f
    join f.item as i
    join i.header as h
    where i.status = :status
    and f.staff = :staff
    order by i.prId desc

解决方案

No. At least, not the way you asked. Once you ask Hibernate to hit the database (with the list() method), Hibernate did its part and the results are now in your hands. You can implement a filtering logic in your code to post-process the results.

That said, it is possible to filter the results in the query itself. If you define a Hibernate filter and enable it for a specific model/query, you'd be able to keep your original HQL query and Hibernate will append it with extra where clauses to further filter the results. See this:

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/filters.html

这篇关于Hibernate过滤查询集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆