Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 918345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:21:37+00:00 2026-05-15T18:21:37+00:00

In my application I have Documents and Comments . A Comment belongs to exactly

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:21:38+00:00Added an answer on May 15, 2026 at 6:21 pm

    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:

    <set ...>
        <filter name="commentMadeAfter" condition=":commentDate <= commentDate"/>
        <filter name="commentMadeBy" condition=":commentUser = userId"/>
    </set>
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.