Lista de consultas de Hibernate dentro de un objeto

Tengo una clase

public class Human{

  @OneToMany
  private List<Pets> myPets;
  //other fields here.
}

Is is it possible to query the human class and get the human with the most number of pets? can Hibernate/HQL do this or do I have to manualy get all humans and query list of pets they contain?? if so how?

preguntado el 23 de abril de 13 a las 13:04

1 Respuestas

HQL allows you to query on the size of a list as follows:

select max(h.myPets.size) from Human h

You can therefore use this as a inner statement to find the human object(s) whose pet.size is equal to the max:

from Human h where h.myPets.size = (select max(h2.myPets.size) from Human h2)

Respondido 23 Abr '13, 13:04

can you explainyour code, it finally worked but a little explanation would not hurt - user962206

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.