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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:28:56+00:00 2026-05-12T13:28:56+00:00

i would very much appreciate your help in transforming the following SQL statement to

  • 0

i would very much appreciate your help in transforming the following SQL statement to a valid HQL Statement. I tried for hours but was not successfull:

SELECT * FROM master as m left outer join (select * from child as c where c.id = (select max(d.id) from child as d where d.MasterFk = c.MasterFk) )as b ON m.id = b.MasterFk;


public class Master {
 private Long id;
 private Collection childs;
 ...
}

public Class Child {
  private Long id;
  private Master master;
}

In the Hibernate Mapping File I map the Master to the Child via a Standard SET (-Mapping) and the relation back from Child to Master is a Many-To-One Association. (This is working.)

==> The objective is to Query the Master with only the latest Child Record (=Only ONE Record!) correctly initialized as the only element in the SET of childs…

Whatever I tried, I failed miserably.

Thank you very much!

  • 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-12T13:28:56+00:00Added an answer on May 12, 2026 at 1:28 pm

    I’m not sure you can do that. The problem I see is not the query, but the initialization. If you don’t return all the objects for the Set, then I’m not sure Hibernate can initialize the Set.

    In Hibernate logic, if an object (here, the Set) is provided (not null, no proxy) in the object graph, then it is supposed to be complete. A Set containing only one element, and thus unable to load the others as it isn’t a proxy, is out of Hibernate logic. In Hibernate logic, a Set is either null, a proxy, or fully loaded (except advanced cases for huge collections, where they are loaded by batches…).

    I understand your need though. Beware, you need to make sure that the codes that will receive this object graph will all understand that the Set is not really one!

    I suggest two alternatives:

    1. Create the object in Java

    An alternative is to query for the data you want, then fill in your objects in your specific Java code.

    The query could be something much simpler, like:

        SELECT m.id, max(c.id) 
        FROM master as m 
        left join m.childs as c 
        group by m.id
    

    The java code would instanciate what you want from that List<Object[]> result.

    Or, you could
    give Master an appropriate constructor, and use new (returning List<Master>):

        SELECT new Master(m.id, max(c.id)) 
        FROM master as m 
        left join m.childs as c 
        group by m.id
    

    2. Add a member

    You could also have a member latestChild on the Master. The mapping could be specified to load it exactly as you need.

    I suggest a formula, which has the advantage to be read-only. Beware that formulas take SQL, not HQL. Insert the SQL that correspond to your subrequest.

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

Sidebar

Ask A Question

Stats

  • Questions 257k
  • Answers 257k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Do you mean the command parameter -projecthelp, this will show… May 13, 2026 at 10:44 am
  • Editorial Team
    Editorial Team added an answer You need to specify the split character as second argument… May 13, 2026 at 10:44 am
  • Editorial Team
    Editorial Team added an answer I like Omar's answer but that's not working for me.… May 13, 2026 at 10:44 am

Related Questions

I was trying to upload my app to the apple store and I faced
Here is what I'm trying to setup My class is named Inventory I have
I am displaying a jpg image (I rotate this by 90 degrees, if this
I am developing a simple platform game in Java using BlueJ. Over the next

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.