9

我有 2 个实体:汽车和车轮(oneToMany),我想取回我的汽车,所有的车轮和(这是棘手的部分)由 wheel.location 排序。下面的代码引发异常,并显示消息“非法尝试取消引用集合”。

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

任何想法如何做到这一点,如果这在 HQL 中是可能的?

4

3 回答 3

9
SELECT DISTINCT c
  FROM Car
       LEFT JOIN FETCH c.wheels AS wheels
ORDER BY wheels.location
于 2009-03-20T14:55:33.537 回答
1

唔。认为您可能需要使用别名?

Select c from Car
       LEFT JOIN FETCH c.wheels wheel
order by wheel.location
于 2009-03-20T14:56:12.833 回答
1

我认为您必须在 from 请求中设置 Car 别名:

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

以下是有关hql 订购的 Hibernate 文档的摘录:

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
于 2012-05-14T10:45:31.780 回答