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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:56:14+00:00 2026-05-11T23:56:14+00:00

I would like to form a query where an associated collection has been restricted,

  • 0

I would like to form a query where an associated collection has been
restricted, ideally with Hibernate Criteria or HQL, but I’d be
interested in how to do this in SQL. For example, say I have a Boy
class with a bidirectional one-to-many association to the Kites class.
I want to get a List of the Boys whose kites’ lengths are in a range.

The problem is that the HQL/Criteria I know only gets me Boy objects
with a complete (unrestricted) Set of Kites, as given in these two
examples (the HQL is a guess). I.e., I get the Boys who have Kites
in the right range, but for each such Boy I get all of the Kites, not
just the ones in the range.

select new Boy(name) from Boy b 
       inner join Kite on Boy.id=Kite.boyId 
             where b.name = "Huck" and length >= 1;

Criteria crit = session.createCriteria(Boy.class);
crit.add(Restrictions.eq("name", "Huck"))
    .createCriteria("kites")
    .add(Restrictions.ge("length", new BigDecimal(1.0)));
List list = crit.list();

Right now the only way I have to get the correct Kite length Sets is
to iterate through the list of Boys and for each one re-query Kites
for the ones in the range. I’m hoping some SQL/HQL/Criteria wizard
knows a better way. I’d prefer to get a Criteria solution because my
real “Boy” constructor has quite a few arguments and it would be handy
to have the initialized Boys.

My underlying database is MySQL. Do not assume that I know much about
SQL or Hibernate. Thanks!

  • 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-11T23:56:15+00:00Added an answer on May 11, 2026 at 11:56 pm

    It turns out that this is best done by reversing the join:

    Criteria crit = session.createCriteria(Kite.class);
    crit.add(Restrictions.ge("length", new BigDecimal(1.0))
    .createCriteria("boy")
    .add(Restrictions.eq("name", "Huck")));
    List<Kite> list = crit.list();
    

    Note that the list’s Kites need to be aggregated into Boys, this
    can be done easily with a HashMap.

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

Sidebar

Related Questions

I would like to form an SQL query to achieve the following: I have
I have 2 tables like which I would like to query to form a
I would like to have Ajax form in Rails so i'm using form_remote_tag. The
I would like to have a form that can submit to two different action
I would like to create a form where a user can enter an arbitrary
I would like to style a form button (input or button tag) with 2
We would like to implement a web form that automatically saves content at regular
I would like to spawn a Windows form from the console using C#. Roughly
I would like to observe changes in multiple form fields and make a call
I would like to plot implicit equations (of the form f(x, y)=g(x, y) eg.

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.