hibernate多表关联查询之DetachedCriteria的查询(非HQL查询方式)
程序员文章站
2022-04-15 23:44:11
...
hibernate 多表关联查询(又称为多表级联查询)
要求:
①必须使用 DetachedCriteria 对象去查询
②禁止使用 HQL 语句
核心代码:
DetachedCriteria dc = DetachedCriteria.forClass(Student.class);
dc.createAlias("team", "t");
dc.createAlias("t.school", "s");
dc.add(Restrictions.like("s.schoolName", schoolName, MatchMode.ANYWHERE));
核心代码等价如下SQL语句:
select
student.*
from
student,
team as t,
school as s
where
student.team_id = t.id
and
t.school_id = s.id
and
s.schoolNam = ?