0

I want to join two table: T1:id,name,email T2:id,address

to join I am doing this:

String hql="select sd.name,tt.name from T1 sd,T2 tt where sd.id=tt.id";
            Query q=ss.createQuery(hql);
            List l2=q.list();
            Object o1=l2.get(0);
            System.out.println("-----------");
            System.out.println(o1.toString());
            System.out.println("-----------");

It is returning object but dont get how to retrieve the values as returned object is not entity object.

Rake
  • 51
  • 7

4 Answers4

1

Every result is an Object Array.The Array's size is the select columns

List l2=q.list();
Object[] o1=(Object[])l2.get(0);//The size is should be 2
System.out.println(o1[0]);
System.out.println(o1[1]);
Conant
  • 60
  • 5
0

You have to cast the Object's in the List to your specific type, Hibernate does not know the type. See What is the "proper" way to cast Hibernate Query.list() to List<Type>? and Hibernate query for selecting multiple values

Community
  • 1
  • 1
Raphael Roth
  • 26,751
  • 15
  • 88
  • 145
0

it will return array of Object which you need to typecast as per your select values like name etc.

List<Object[]> values= (List<Object[]>)q.list();
     for(Object[] val: values){

....
//your specific type values
}
Sai prateek
  • 11,842
  • 9
  • 51
  • 66
0

Another option is creating a view with your joined tables, then, create hibernate entity base on that view

VinhNT
  • 1,091
  • 8
  • 13