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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:06:14+00:00 2026-05-26T06:06:14+00:00

I am confused about how to query for a list of objects with sub-objects

  • 0

I am confused about how to query for a list of objects with sub-objects in it.

I have two classes Execution and Order. And In Execution, I have the order object in it as a one-to-one relationship:

public class Execution {
    private long id;
    private Order order;
    ...

In the Execution.hbm.xml, I have configured Order as a many-to-one with unique=”true”.

<class name="Execution" table="executions">
    <id name="executionId" type="long">
        <generator class="native" />
    </id>
    <many-to-one name="Order" unique="true" fetch="join" class="Order" />
</class>

Normally, we query for Execution objects with HibernateDAOSupport by using:

List<Execution> executions = getHibernateTemplate().find("from Execution");

With the above relationship, how do we query it and where can I find more information on persisting objects for complicated joins? I’ve tried:

List<Execution> list = getHibernateTemplate().find("from Execution e left outer join e.order");

But it’s giving a ClassCastException as the query does not seem to return Execution objects with the extra order details in the Order object in Execution.

Is something wrong with the mapping? Or the query?

Thanks so much for any pointers!

— update

Sorry for being a noob. Apparently the first query works. After reading tons of documentation on HQL, they weren’t clear enough about this. Any better documentation would be helpful. 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-26T06:06:15+00:00Added an answer on May 26, 2026 at 6:06 am

    This will not work, because you do not explicitly tell that you want only Execution entities in your result. It cannot figure it out, because of join to the order entity causes also Order attributes to be part of result.

    List<Execution> list = getHibernateTemplate().find(
        "from Execution e left outer join e.order");
    

    Just add select and it will work fine:

    List<Execution> list = getHibernateTemplate().find(
        "select e from Execution e left outer join e.order");
    

    But does it make sense to have a such a query is other question, in general you do not have to make joins in queries to have relationship attributes populated. Are they populated is controlled by setting fetching strategy to LAZY/EAGER or by depending about defaults.

    In your case order should exist in query only if

    1. you want to limit results of query by some attribute in order (in such a case you can also avoid JOIN in your HQL query and just navigate through the path: e.order.id=2) or
    2. relationship to order is lazy and you just want to ensure that order
      is fetched:

      List list = getHibernateTemplate().find(
      “from Execution e left outer join fetch e.order”);

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

Sidebar

Related Questions

I'm confused about how DQL works, and really need some help here. I have
I am confused about why I am getting rows returned from a query... Here
I am confused about selecting two approaches. Scenario there are two tables Table 1
I am confused about something really simple in ASP.NET. I have seen many times
I am a bit confused about SQL CLR. When i use the below query
Im getting very confused with indexes in MySQL. I have two tables: TableA1 and
I'm confused about how to change this query to LINQ select CONTENT from (
I Am Confused about Why It is Called as A Phantom Query.. eg Assume
I'm a bit confused about many to many relationship tables, and the code that
I find I'm confused about lazy loading, etc. First, are these two statements equivalent:

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.