HQL order by within a collection

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

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 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!