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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:49:46+00:00 2026-05-21T04:49:46+00:00

Using hibernate ctiteria I want to select an object and it’s associated oneToMany list

  • 0

Using hibernate ctiteria I want to select an object and it’s associated oneToMany list of objects. I want to paginate through this list avoiding the dreaded hibernate n+1 select issue

Here’s a working solution which requires 11 trips to the database for 10 parent objects.

Criteria criteria = this.getSession().createCriteria(Mother.class);
criteria.addOrder(Order.asc("title"))
.setMaxResults(details.getMaxRows())
.setFirstResult(details.getStartResult())
.setFetchMode("kittens", FetchMode.SELECT);
List test = criteria.list();

And here’s a solution which executes only one sql statement (hurray) but cannot handle pagination ie the setMaxResults and setFirstResult are incorrect on the parent object Mother (boo)

Criteria criteria = this.getSession().createCriteria(Mother.class);
criteria.addOrder(Order.asc("title"))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.setMaxResults(details.getMaxRows())
.setFirstResult(details.getStartResult())
.setFetchMode("kittens", FetchMode.JOIN);
List test = criteria.list();

This seems like such a common requirement but I’ve dug around for a solution with no luck.

Any takers?

  • 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-21T04:49:47+00:00Added an answer on May 21, 2026 at 4:49 am

    Getting it down to 1 query is tough (i.e. I don’t know a portable solution), but getting it down to 2 queries (irrespective of n) is pretty simple:

    Criteria criteria = this.getSession().createCriteria(Mother.class);
    criteria.addOrder(Order.asc("title"))
        .setMaxResults(details.getMaxRows())
        .setFirstResult(details.getStartResult())
        .setProjection(Projections.id());
    List<?> ids = criteria.list();
    
    criteria = getSession().createCriteria(Mother.class)
        .add(Restrictions.in("id", ids))
        .setFetchMode("children", FetchMode.JOIN)
        .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
    
    return criteria.list();
    

    For some databases, subselect fetching children might work, too.

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

Sidebar

Related Questions

I want to write following sql using projection in hibernate SELECT id, name, MAX(ver)
Using Hibernate 's Criteria API , I want to select the record within a
Using Hibernate's Criteria, I want to execute the equivalent of: select distinct uspscity, state
I'm using Hibernate Criteria API in my project. In this API, list() method is
I want fetch last record using hibernate criteria Can u help me?
Using hibernate, how can I persist a class with a List<String> field? Consider the
Possible Duplicate: How do we count rows using Hibernate? I want count number of
We're using Hibernate to load objects that meet certain criteria. If the user decides
I am using spring with hibernate. my database is oracle. i want to update/save
I'm trying to get a collection of objects using Hibernate, but I am not

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.