hibernate3中的离线查询1

假设要通过stuName查询一个学生Student记录,可以如下:
DetachedCriteria dc = DetachedCriteria.forClass(Student.class);
dc.add(Restrictions.like("stuName", stuName, MatchMode.ANYWHERE));
   
如果要通过Student的Team的teamName查询一个Student记录:
DetachedCriteria dc = DetachedCriteria.forClass(Student.class);
dc.createAlias("team", "t");
dc.add(Restrictions.like("t.teamName", teamName, MatchMode.ANYWHERE));


这里有一个特殊情况,如果是对引用对象的id查询,则可以不用建立引用,也就是可以不调用createAlias()语句,如下所示:
DetachedCriteria dc = DetachedCriteria.forClass(Student.class);
dc.add(Restrictions.like("team.id", teamId, MatchMode.ANYWHERE));


多级查询:
DetachedCriteria dc = DetachedCriteria.forClass(Student.class);
dc.createAlias("team", "t");
dc.createAlias("t.school", "s");
dc.add(Restrictions.like("s.schoolName", schoolName, MatchMode.ANYWHERE));


你可能感兴趣的:(hibernate3)