简体   繁体   中英

How to make complex query when using "hibernate filter"?

I and my colleague are working to make a framework for our company. We are using Hibernate as DAO and Spring as IOC. We would like to use hibernate filters to reduce our result set based on some restrictions, but we are confused how to use it properly!

Consider entity A has reference to B and B to C and C to D ant etc... How can we write a filter that when i search the A entity, all the filters defined on B, C and D applied.

We have used Hibernate filters in very cases but we have not already succeeded in such these cases. Have we got any other solutions for filtering data ?

I stuck into similar problem recently. The only solution I've found is to write a full SQL with all conditions which you need and retrieve all IDs of A (if we're creating a filter in the A entity) which matches your requirements and then in the filter check, whether ID is in those IDs or not. It would be something like

@FilterDef(name = "filterName", 
           parameters = {@ParamDef(name = "param", type = "string")},
           defaultCondition = "id in (select a.id from A a
                                      join B b on a.b_id = b.id
                                      join C c on b.c_id = c.id
                                      where c.some_property = 2)")

However, I'm not sure this is a good solution for some large queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM