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?

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Nan
  • 63
  • 1
  • 9
  • 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: http://stackoverflow.com/questions/26139386/hql-query-using-dot%C2%B4s-in-the-response-parameters – Tom Oct 24 '16 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? – Nan Oct 24 '16 at 13:13
  • Is there a more beautiful method than doing a Java loop? – Nan Oct 24 '16 at 20:45

1 Answers1

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);
}
Nan
  • 63
  • 1
  • 9