可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
SELECT DISTINCT c FROM Car LEFT JOIN FETCH c.wheels AS wheels ORDER BY wheels.location
回答2:
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
回答3:
Hmm. Think you might need to use an alias?
Select c from Car LEFT JOIN FETCH c.wheels wheel order by wheel.location