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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:52:43+00:00 2026-05-18T21:52:43+00:00

The title doesn’t tell the complete story. Please read the message. I have two

  • 0

The title doesn’t tell the complete story. Please read the message.

I have two objects: Adult and Child. Child has a boolean field isMale, and a reference to Adult. Adult doesn’t reference Child.

public class Adult {
   long id;
}
public class Child {
   long id;
   boolean isMale;
   Adult parent;
}

I want to create a query to list the number of sons each adult has including adults who don’t have any sons. I tried:

Query 1

SELECT adult, COUNT(child) FROM Child child 
RIGHT OUTER JOIN child.parent as adult 
WHERE child.isMale='true' 
GROUP BY adult

which translates to sql

select
    adult.id as col_0_0_,
    count(child.id) as col_1_0_,
    ... {omit properties}
from
    Child child
right outer join
    Adult adult
        on child.parentId=adult.id 
where
    child.isMale = 'true'
group by
    adult.id

Query 1 doesn’t pick up adults that don’t have any sons.

Query 2:

SELECT adult, COUNT(child.isMale) FROM Child child 
RIGHT OUTER JOIN child.parent as adult 
GROUP BY adult

translates to sql:

select
    adult.id as col_0_0_,
    count(child.id) as col_1_0_,
    ... {omit properties}
from
    Child child
right outer join
    Adult adult
        on child.parentId=adult.id 
group by
    adult.id

Query 2 doesn’t have the right count of sons. Basically COUNT doesn’t evaluate isMale.
The where clause in Query 1 filtered out Adults with no sons.

How do I build a HQL or a Criteria query for this use case?

Thanks.

  • 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-18T21:52:43+00:00Added an answer on May 18, 2026 at 9:52 pm

    Condition child.isMale='true' is false for adults without children (isMale is NULL), therefore you should handle this case:

    SELECT adult, COUNT(child) 
    FROM Child child RIGHT OUTER JOIN child.parent as adult 
    WHERE child IS NULL OR child.isMale='true'  
    GROUP BY adult
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The title doesn't give justice to the question. I have a site and I
Sorry if the title doesn't make sense. Basically I have a series of strings
i read the question with a similar title but it doesn't match my problem.
How come the AlertDialog that has the title Location was saved to file doesn't
The title doesn't actually fully describes the problem, that is: I have a table
As stated in title, I have a form that doesn't have any control on
As title. ruby test/functionals/whatevertest.rb doesn't work, that requires me to replace all require 'test_helper'
I'm trying to hide the Title field in a list. This doesn't seem to
Title might be a bit confusing, so let me explain. I have a website
Forgive me if the title doesn't exactly match what I'm asking, I'm having a

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.