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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:50:48+00:00 2026-05-17T06:50:48+00:00

This might be a super easy answer, since I’m sure it’s not uncommon. I

  • 0

This might be a super easy answer, since I’m sure it’s not uncommon. I see some similar questions, but nothing that seems to describe my problem.

I have two objects: a car and person. They’re in a many-to-many relationship, i.e. a car can be owned by multiple people, and a person can own multiple cars. Car has a lazily initialized set of Drivers:

@ManyToMany(targetEntity=Person.class, fetch=FetchType.LAZY)
private Set<Person> people;

I want to find all blue cars with owners under the age of 25:

Criteria foo = persistenceManager.createCriteria(Car.class, "myCarAlias");
Criteria bar = foo.createCriteria("drivers", "myDriverAlias");
//add restrictions on foo and bar for blue and age, respectively
baz = foo.list();

My problem is when I iterate through the list and call getDrivers() on each car, it initializes the collection and gets all of that car’s drivers. I guess the .list() I ran doesn’t set the results on each car.

I get back the results I expect if I manually run the SQL hibernate is generating, so I’m reasonably sure the criteria isn’t the issue.

I can understand why this happens, but I’m not sure how to get around it without iterating over each car and running a query for drivers on each one. I’d like to avoid doing that if possible and would appreciate any advice.

Thanks in advance!

  • 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-17T06:51:04+00:00Added an answer on May 17, 2026 at 6:51 am

    Were you hoping that the Drivers collection for each Car would only contain People who matched your criteria (i.e. are over 25)? In other words, you don’t want the Drivers collection containing people who are over 25?

    If that is the case then you will need to use a ResultTransformer (see section 15.4 of the Hibernate documentation) because Hibernate does not pre-filter collections before returning the results.

    If however the problem is the Drivers collection is being lazy loaded and that is not desirable then setting the fetch mode (I typically use JOIN since IIRC FetchMode.EAGER was deprecated) should solve the problem:

    baz = persistenceManager.createCriteria(Car.class, "myCarAlias")
        .setFetchMode("drivers", FetchMode.JOIN)
        // add additional restrictions here
        .list();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might seem like a very easy question for some of you folks, but
I know, that this might be super easy, but probably, working late just halted
This might be a super simple bug, but my eyes are practically bleeding from
I'm a newbie with PHP so this might be ridiculously easy question, but despite
this might sounds silly but I am super confused. I want to install drupal
I realise that this might be a stupid question, but I have not come
This might be a dumb question but I can't get this super simple script
I originally asked this question on Super User but was told that it might
My problem might seems easy, but I really can't resolve it. At some point,
This might sound super crazy but I really want to know if this can

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.