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 367175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:42:10+00:00 2026-05-12T13:42:10+00:00

I have this hql query, which works perfect: select m from Media m join

  • 0

I have this hql query, which works perfect:

            select m 

            from Media m

            join m.Productlines p
            join m.Categories c                
            join m.Spaces sp
            join m.Solutions so

            where m.Uid != 0
            and p.Uid in (:productlines)
            and c.Uid in (13)                
            and sp.Uid in (52)
            and so.Uid in (15,18)

            group by m.Uid

But now it needs to be parameterized/made dynamic, not only the parameters, but also the joins (it is possible to select only from Media, without any joins, and so no *.Uid in will be required in this case).

I dont want to mess around with a StringBuilder instance and build the hql query that way, I would rather like to use the Criteria API, but I cant get a

SELECT m.*
....
GROUP BY m.Uid

query to work with Criteria.

If I add a

Projections.GroupProperty("Uid") 

to my query, nhibernate selects

SELECT m.Uid
....
GROUP BY m.Uid

which is of course wrong.

After that, I also need to count the unique rows the query returned, as the result is paged.

So, my other query is quite similiar, but I cant find a Criteria equivalent for

SELECT COUNT(DISTINCT m.Uid)

Here is the HQL:

            select count(distinct m.Uid) 

            from Media m

            join m.Productlines p
            join m.Categories c                
            join m.Spaces sp
            join m.Solutions so

            where m.Uid != 0
            and p.Uid in (:productlines)
            and c.Uid in (13)                
            and sp.Uid in (52)
            and so.Uid in (15,18)

How can this be done with Criteria API?

Please, (N)Hibernate experts – help me with this, I cant find a working solution. Any help is greatly appreciated!

  • 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-12T13:42:11+00:00Added an answer on May 12, 2026 at 1:42 pm

    Group columns are implicitly returned as result, but you can add more columns. AFAIK, you can return full entities:

    var query = session.CreateCriteria(typeof(Media), "m")
      .Add(Projections.GroupProperty("m"))
      .Add(Restrictions.NotEq("m.Uid", 0));
    
    // dynamically add filters
    if (filterProductLines)
    {
      query
        .CreateCriteria("m.Productlines", "p")
        .Add(Restrictions.Eq("p.Uid", productLines));
    }
    // more dynamic filters of this kind follow here...
    
    IList<Media> results = query.List<Media>();
    

    To count the full number of results you can just build up the same query with different projection:

    var query = session.CreateCriteria(typeof(Media), "m")
        .SetProjection(Projections.CountDistinct("m.Uid"));
    // rest of the query the same way as above
    
    long totalNumberOfResults = query.UniqueResult<long>();
    

    I’m getting unsure about the Projections.GroupProperty("m"), you need to try this. If it doesn’t work, you could make it an DetachedQuery that only returns ids:

    var subquery = DetachedCriteria.For(typeof(Media), "m")
      .Add(Projections.GroupProperty("m.Uid"))
      .Add(Restrictions.NotEq("m.Uid", 0));
    // add filtering
    
    var query = session.CreateCriteria(typeof(Media), "outer")
      .Add(Subqueries.PropertyIn("outer.Uid", subquery));
    
    IList<Media> results = query.List<Media>();
    

    This creates a sql query like this:

    select outer.* // all properties of Media to create an instance
    from Media outer
    where outer.Uid in (
      select Uid
      from media m
      where // filter
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With this HQL query: SELECT DISTINCT fs FROM FileStatus fs WHERE UPPER(STR(fs.filePath)) LIKE :FILE_PATH
I'm trying to write an HQL query to select objects which contain an object
I am trying to write a HQL Query which selectes rows from a table
I have this RewriteRule that works too well :-) RewriteRule ^([^/]*)/$ /script.html?id=$1 [L] The
I am trying to write an hql query which gives me the number of
I have a question with HQL query and hibernate. I have a user class
I have spent several hours with this SQL problem, which I thought would be
I am new to HQL. This is a mysql Query. I need to convert
I'm writing an HQL query, in the form of: from a where ... or
I have this code in jQuery, that I want to reimplement with the prototype

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.