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

  • SEARCH
  • Home
  • 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 412067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:02:56+00:00 2026-05-12T18:02:56+00:00

Let’s say I have entities A, B, C and each A has many B

  • 0

Let’s say I have entities A, B, C and each A has many B and C entities. I want to query a load of A entities based on some criterea, and I know I will be accessing all B and C entities for each A I return.

Something like select a from A as a join fetch a.b join fetch a.c would seem to make sense at first, but this creates a huge product if the numbers of B and C entities are large. Extending this to another associated entities makes the query totally unreasonable.

If I leave JPA to its own devices, I end up with n+1 selects when it wants to access the B and C entities.

What I thought I’d do was query A join fetch B, then A join fetch C, but this doesn’t work as it gives me two List<A> results each with only half the information.

This is a pretty simple query in SQL terms, and I’m disappointed there isn’t an obvious way to handle this. Am I missing something?

Provider is toplink essentials

  • 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-12T18:02:56+00:00Added an answer on May 12, 2026 at 6:02 pm

    I wonder why you say this is pretty simple in SQL terms. Wouldn’t you also have the cartesian product?


    Using the Hibernate provider for JPA, an option you mention works:

    query A join fetch B, then A join fetch C

    You have two list of the same values, you use only one and it is fine (you just need to LEFT join).


    In Hibernate, you can also ask to fetch the missing data in a second query.

    Use fetch="subselect".

    See https://www.hibernate.org/315.html


    UPDATED after comment of the Original Poster:

    In java, you could also do this by hand.

    1. Fetch the As with their collections of Bs, in a list called entityAs.
    2. Fetch the As with their collections of Cs (reusing part of the query, or using ids).
    3. Create a datastructure Map> for the second query (for performance, to avoid inner loop).
    4. Loop on list entityAs, using the Map to set the set Cs for each instance A.

    This would have a good performance also.

    If you run several times into this need, you could write a parameterized method to do this for you, so you only code it once.

    As commented by the Original Poster, you need to detach all A entities from entityAs before modifying them, to be sure there will be no update send to the database…

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

Sidebar

Related Questions

Let's say I have a drive such as C:\ , and I want to
Let's say I have one class User, and it has a property of type
Let's say you have a class library project that has any number of supplemental
Let's say I have the string: hello world; some random text; foo; How could
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's say i have this block of code, <div id=id1> This is some text
Let's say that I have a model that handles recipes, and I want to
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName

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.