HQL在集合中的顺序

时间:2009-03-20 14:38:19

标签: hibernate jpa java-ee hql

我有2个实体:汽车和轮子(oneToMany),我想要检索我的汽车,所有车轮和(这是棘手的部分)由wheels.location命令。下面的代码抛出一个异常,消息“非法尝试取消引用收集。”

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

知道如何做到这一点以及在HQL中是否可行?

3 个答案:

答案 0 :(得分:9)

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

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

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

以下摘自hql ordering上的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