In my application I have Documents and Comments. A Comment belongs to exactly one Document, a Document can have several Comments.
I have a fairly complicated named query on my Documents:
<query name="public.documents">
<![CDATA[
from Document d join d.someOtherProperty join ...
where
...
]]>
</query>
The result set of this query should now be filtered according to several criteria. Unlike all the examples I could find on Hibernate Filters these properties are not in the Document class but in the Comment class. For example I would like to be able to add a filter which only shows me the Documents from the result set which have Comments by a particular author or which have Comments that were added on a certain date. Or both aforementioned restrictions.
Right now I do it like this:
<query name="public.documents.restricted.to">
<![CDATA[
from Document d join d.someOtherProperty join ...
where
...
AND d.id IN (:restrictedTo)
]]>
</query>
This is a very ugly way to achieve my goal. Can I do this with a hibernate filter? I understood that filter is only a thin wrapper for additional where arguments, in my case I would need some sort of join.
If it can’t be done using a filter can you point me towards a different solution, I think this is a fairly common problem and I think that writing a query for each and every possible combination of restriction criteria is far from elegant.
I have not tried it, but I believe you can do this with filters.
As well as filtering entities, filters can be used to filter collections, such as the collection of Comments related to a Document.
You define filters for the Comments and activate these, something like this on the Doucment entity:
This will then restrict the comments retrieved on each document to the specified criteria. To ensure the returned documents only contain comments that match this criteria, you also add a filter (or a where clause to the query) to exclude documents with no comments from the result set.
There is an example of something similar on the spring forums.