在使用交叉表的时候,对动态字段进行排序,用 sql 是 left join 值表 以排序的值类型为过滤条件,然后对值列排序, hql 也是left join ,不用on,hibernate会自动加上,你如果需要添加其他的条件,可以用with,另外 hql 可以对 one-to-many 的属性(如Set) 直接当做条件来过滤 例如
学生表 对应 Student 类
Student{
Integer id;
Set scores; // 所有该学生下的成绩集合
}
成绩表 对应 Score 类
Score{
Integer id;
Integer value; // 成绩的值
Student student; // 该成绩属于哪个学生
}
当需要查询学生,又需要对学生的某科成绩进行排序的话可以这样
select stu from Student as stu
left join stu.scores as sc
with sc.id=1
where ....
order by sc.value
学生表 对应 Student 类
Student{
Integer id;
Set scores; // 所有该学生下的成绩集合
}
成绩表 对应 Score 类
Score{
Integer id;
Integer value; // 成绩的值
Student student; // 该成绩属于哪个学生
}
当需要查询学生,又需要对学生的某科成绩进行排序的话可以这样
select stu from Student as stu
left join stu.scores as sc
with sc.id=1
where ....
order by sc.value
本文介绍如何在HQL中实现对动态字段的排序,特别是在一对多关系的数据表中,如学生表与成绩表之间的关联。通过具体例子展示了如何利用left join进行有效排序。
3422

被折叠的 条评论
为什么被折叠?



