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.

4
  • Maybe this answers your question stackoverflow.com/questions/13607337/… Commented Mar 21, 2016 at 6:13
  • I'm certain there are better, more automated ways, to do this but as written it's a little difficult to gleam what you're actually asking. Commented Mar 21, 2016 at 6:18
  • @Rubio:Thanks man! It helps a lot Commented Mar 21, 2016 at 6:23
  • @Rake You're welcome :-) Commented Mar 21, 2016 at 6:24

4 Answers 4

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]); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks ... it is like RubioRic's ans.
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

Comments

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 } 

1 Comment

Thanks ... it is like RubioRic's ans
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.