I have 2 entities: car and wheels (oneToMany) and I want to retrieve my car, with all the wheels and (this is the tricky part) ordered by wheels.location. The below code throws an exception with the message "illegal attempt to dereference collection."
Select c
from Car
LEFT JOIN FETCH c.wheels
order by c.wheels.location
Any idea how to do this and if this is possible in HQL?
SELECT DISTINCT c
FROM Car
LEFT JOIN FETCH c.wheels AS wheels
ORDER BY wheels.location
I think that you have to set the Car alias in the from request:
SELECT DISTINCT c
FROM Car c
LEFT JOIN FETCH c.wheels AS wheels
ORDER BY wheels.location
Below is an excerpt from the Hibernate documentation on 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With