0

Img problem org.hibernate hibernate-core 4.3.8.Final org.hibernate hibernate-entitymanager 4.3.8.Final My pom.xml

My Problem is: How to make a query like this...

 SELECT
    TABLE_D.*,
    TABLE_A.NAME_A

FROM
    TABLE_D
INNER JOIN
    TABLE_E
        ON TABLE_D.ID_TAB_E  = TABLE_D.ID_TAB_D
LEFT JOIN
    TABLE_C 
        ON TABLE_C.ID_TAB_C  = TABLE_D.ID_TAB_D
INNER JOIN
    TABLE_B
        ON TABLE_B.ID_TAB_B  = TABLE_C.ID_TAB_C
INNER JOIN
    TABLE_A
        ON TABLE_A.ID_TAB_A  = TABLE_B.ID_TAB_B
WHERE
    TABLE_A.NAME_A = "XXXX";

And Return the selected the values TABLE_D and TABLE_A ​​in a unique Object List(ex: Object that i create to take all this fields) (I could create 1 filter, whatever...) in the JPA ? Plz Help.

  • Show your entity classes – Lalit Rao May 07 '15 at 20:38
  • They are so big, but i just need one class to take these fileds. when i use getConnection().createNativeQuery(sql.toString(), Table_D.class), it returns only the TABLE_D fields and your fk entity TABLE_E and TABLE_C. So i cant read TABLE_B and A. – user2824425 May 08 '15 at 00:02

1 Answers1

1

If you need to return a list of selected columns in HQL you can just write your hql query and return a List of Object array, i.e.:

List<Object[]> result = session.createQuery("select a.field1, b.field2 from EntityA a join a.entityB b").list();

then you can iterate and get values, based on their type (i.e. String):

for (Object[] arr : result) {
    String col1 = (String)arr[0];
    String col2 = (String)arr[1];
}
RedPelle
  • 285
  • 4
  • 17
  • can i use the same ideia with createNativeQuery() ? – user2824425 May 07 '15 at 23:55
  • Yes, just the column type could be different in createQuery and createNativeQuery, i.e. instead of an Integer it could be a BigInteger, while doing casting. But you have no reason to use native SQL because your query is full supported by hql and jpql... – RedPelle May 08 '15 at 06:14