2009-03-20 11 views
9

Tengo 2 entidades: coche y ruedas (oneToMany) y quiero recuperar mi coche, con todas las ruedas y (esta es la parte difícil) ordenado por wheels.location. El código siguiente arroja una excepción con el mensaje "intento ilegal de eliminación de referencia".orden de HQL dentro de una colección

Select c 
    from Car 
     LEFT JOIN FETCH c.wheels 
order by c.wheels.location 

¿Alguna idea de cómo hacer esto y si esto es posible en HQL?

Respuesta

9
SELECT DISTINCT c 
    FROM Car 
     LEFT JOIN FETCH c.wheels AS wheels 
ORDER BY wheels.location 
+1

Puede especificar ascendente y orden descendente usando 'ASC' y' DESC'. p.ej. '... PEDIDO POR DESC'. –

1

Hmm. ¿Crees que necesitarías usar un alias?

Select c from Car 
     LEFT JOIN FETCH c.wheels wheel 
order by wheel.location 
1

creo que usted tiene que establecer el alias de coches en el de solicitud:

SELECT DISTINCT c 
    FROM Car c 
     LEFT JOIN FETCH c.wheels AS wheels 
ORDER BY wheels.location 

A continuación es un extracto de la documentación de Hibernate en hql ordering:

select cat from Cat cat 
     join cat.kittens kitten 
group by cat.id, cat.name, cat.other, cat.properties 
having avg(kitten.weight) > 100 
order by count(kitten) asc, sum(kitten.weight) desc 
Cuestiones relacionadas