hql request:manytomany集合属性的订单列表结果

时间:2014-02-07 15:44:54

标签: java spring hibernate

我有一个hql请求,如下所示:

select Distinct prof 
from Professor as prof 
left join fetch prof.students as stud

教授和学生处于多种关系中。 我想按学生的名字订购教师名单。

我试过了:

select Distinct prof
from Professor as prof
left join fetch prof.students as stud
order by prof.students.name

我收到了错误: 严重:org.springframework.orm.hibernate3.HibernateQueryException:非法尝试解除引用集合

我也尝试过:

select Distinct prof
from Professor as prof
left join fetch prof.students as stud
order by stud.name

我收到了错误: 严重:org.springframework.dao.InvalidDataAccessResourceUsageException:无法执行查询;

我的订单是否可以通过?或者hibernate映射不允许这样的请求?

1 个答案:

答案 0 :(得分:0)

从查询中删除Distinct关键字。然后,如果您需要消除重复项,请使用ResultTransformer为您合并结果集。

String hql = "select prof from Professor as prof left join fetch prof.students as stud order by stud.name"; 
Query query = session.createQuery(hql)
                     .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);