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.