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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:35:55+00:00 2026-05-25T12:35:55+00:00

I have a bi-directional one-to-many relationship defined between Parent and Child classes. I’m looking

  • 0

I have a bi-directional one-to-many relationship defined between Parent and Child classes. I’m looking to execute a query such that I can return a single parent, and a subset of its children in a bean.

public class Parent {
    private int id;
    private Set<Child> children = new HashSet<Child>(0);

    // other fields + getters and setters
}

public class Child {
    private Parent parent;
    private int age;

    // other fields + getters and setters
}

The output I’m looking to achieve is:

public class ParentChildWrapper {
    private Parent parent;
    private Collection<Child> children; // subset of children
}

I’d like to filter on the parent’s id and the child’s age column. I’ve tried a number of query combinations in order to get the desired output.

The following is close, but doesn’t group the children in a collection in the ParentChildWrapper bean I’ve defined. The result I get is a list of objects, with 1 instance per child that matches the age filter:

Query q = session.createQuery(
    "SELECT p, c " +
    "FROM Parent p " +
    "INNER JOIN p.children c " +
    "WHERE p.id = :id " +
    "AND c.age = :age"
);

I’ve also tried to group by the parent in order to try and aggregate all the children into a collection, also to no avail.

Obviously I could separate this into two separate queries in order to select the parent, and then select the children that I want. But it feels like this should be a fairly common use case. Perhaps I’m not thinking in a hibernate-esque way.

  • 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-25T12:35:56+00:00Added an answer on May 25, 2026 at 12:35 pm

    Just don’t use any transformer and code the loop yourself:

    List<Object[]> rows = query.list();
    Parent parent = null;
    List<Child> children = new ArrayList<Child>(rows.size());
    for (Object[] row : rows) {
        parent = (Parent) row[0];
        children.add((Child) row[1]);
    }
    ParentChildWrapper result = new ParentChildWrapper(parent, children);
    

    This makes 8 lines of trivial code instead of 1, but avoids reflection calls, and thus allows safe refactoring.

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

Sidebar

Related Questions

Say I have a Customer - CustomerOrder one-to-many bi-directional relationship with the CustomerOrder holding
I have a basic has_many :through relationship that is bi-directional: calendars have many calendar_calendar_events
I have the same tables as in this question: MySql query over many-to-many relationship
Let's say I have two classes, Parent and Child. These two are related via
I am trying to have a many to many relationship between players and fixtures.
I have a one-directional many-to-many association: the ListDefinition class has the Columns property of
I have two tables, one containing cities and one containing countries. Those are bi-directional
If I have a relationship between two tables (both tables have their own primary
My basic scenario is a bi-directional many-to-many relationship that is being mapped with JPA.
We have 4 Tables, One A to One B One B to Many C

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.