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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:03:47+00:00 2026-06-07T19:03:47+00:00

I have three entities as follows: public class EntityA { private Long id; //Getters

  • 0

I have three entities as follows:

public class EntityA
{
    private Long id;
    //Getters and setters
}

public class EntityB
{
    private Long id;
    private EntityA entitya;
    //Getters and setters
}

public class EntityC
{
    private Long id;
    private BigDecimal amount;
    private EntityB entityb;
    //Getters and setters
}

Now, given an Instance of EntityA, i want to get a list of EntityC. I have two options available to me currently. I don’t know which one is more optimized. The options are:

1.
select c from EntityC c where c.entityb in (select b from EntityB b where b.entitya = :entitya)

2.
Add a new property to EntityB

private Set<EntityC> entityCCol;

@OneToMany(mappedBy="entityb")
public Set<EntityC> getEntityCCol()
{
   return entityCCol;
}

select b from EntityB a join fetch a.entityCCol b

Which of these two queries is easier and optimized?

  • 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-06-07T19:03:48+00:00Added an answer on June 7, 2026 at 7:03 pm

    It depends on the size of the collection. For small collections I would use the relationships in the object model. More from a design/usability perspective than performance, it is more object-oriented. I wouldn’t join fetch it though, just access the model normally. You should probably also have a relationship from A to B to make your model more useful.

    For the query in #1, your query is not very efficient using the sub-selects, just use a join,

    select c from EntityC c where c.entityb.entitya = :entitya

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

Sidebar

Related Questions

I have three entities: EntityA, EntityB and EntityC connected with to-many relationships. See schema
I have three mappings as follows: public MainChapterMap() { // other properties HasMany(x =>
I have following entities: public class Product { [Key] public int Id{get;set;} //other properties
I have three entities: Country, State and City with the following relationships: When creating
I have three entities. Author Newspaper Dissertation Newspaper and Dissertation both can have one
I have a case where i have three entities with one-to-many and one-to-many relationships:
I have a file called entities.py, which contains the following code: class EntityClass: entities
I'm using Doctrine 2 ORM and I have this problem. I have three Entities,
I have three entities in my music-related DB: Video , Album and Genre .
I have three entities like this. Employee: id name EmployeeDepartment: id departmentID employeeID Department:

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.