2

I am learning Hibernate, and i am having problem with the join. The problem is that, the query return an List of Objects and i don't know how i must manage it

This is the code:

Query query = session.createQuery("select p.id, p.pokemon, t.types from Pokedex as p JOIN p.assPokTypes t where p.id = '001'");
List lista = query.list();

for(Object row:lista) {
   System.out.println(row);
}

This is the table structure:
Pokedex (ID, pokemon)
AssPokTypes (id_pokemon, id_type, primary_type)

Thanks

vecio88
  • 95
  • 1
  • 11

2 Answers2

2

Actually it returns a list of Object array not Objects

        List<Object[]> lista = query.list();

      for(Object[] row:lista) {
        System.out.println(row[0]);//Object of the first column
        System.out.println(row[1]);//Object of the second column
       }
Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
  • No, is a Object. If i try to apply your code, Eclipse give me this error "The type of the expression must be an array type but it resolved to Object" – vecio88 Oct 11 '16 at 12:03
  • My mistake , i edited the answer , array of object is an object thats why it can iterate on it – Amer Qarabsa Oct 11 '16 at 12:05
  • Work!! :) therefore, I have a List of Object[ ] that in turn are Object[ ]..right? – vecio88 Oct 11 '16 at 12:10
  • Yes, You will notice that if you are using JPQL even you dont use a typed query here you wont get an "unchecked" warning , the underlying JPA layer knows that the result list will be of type Object[] – Amer Qarabsa Oct 11 '16 at 12:29
0

Every object from the list is column. row[0] - p.id row[1] - p.pokemon .... Iterate list and use columns that you need.

Hibernate: How to get result from query with multiple classes

Community
  • 1
  • 1