Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

java - Hibernate filtering query collections

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...