0

I would like to do something like this with an HQL query:

SELECT new Table1(a.field1, a.field2, new Table2(b.field1, b.field2, b.field3)) 
FROM Table1 a INNER JOIN a.table2 as b where...

Do you know how I can do it?

3
  • Okay, so we don't know how many attributes you Table1 and Table2 have. Anyway, you can find some nice examples here in SO. This could already help you: stackoverflow.com/questions/26139386/… Commented Oct 24, 2016 at 13:00
  • I have n fields inside my tables. What I want to do is not retrieving any fields, but only fields that I need. Are we obliged to select fields in HQL and then doing a loop in Java like in your example? Commented Oct 24, 2016 at 13:13
  • Is there a more beautiful method than doing a Java loop? Commented Oct 24, 2016 at 20:45

1 Answer 1

1

So I've found a pretty solution :

SELECT new Table1(a.field1, a.field2, a.field3, b.field1, b.field2) 
FROM Table1 a INNER JOIN a.table2 as b where...

With a constructor in Table1 like :

Table1(afield1, afield2, afield3, bfield1, b.field2) {
   this.afield1 = afield1;
   this.afield2 = afield2;
   this.afield3 = afield3;
   this.table2 = new Table2(bfield1, b.field2);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.