微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Hibernate条件查询多个条件

在我目前的项目中,我遇到了使用hibernate条件查询获取实体的问题.我有以下实体:

>教授,其中包含学生名单
>学生,其中包含作业列表.
>分配,包含分配给它的学生的ID.

现在,我希望获得与教授相关的所有作业,即教授分配给他的学生的所有作业.

查询显示我要在条件查询中实现的内容.

select * from Assigment p,Student a,Professor c where p.studentid = a.id and a.proffid = c.id and c.id = 2411;

如何使用hibernate criteria API实现此查询

最佳答案
假设你的表是这样的:

@Entity
public class Professor{
    K id;
    List

使用别名的简单示例:

Criteria criteria = currentSession.createCriteria(Professor.class,"professor");
    criteria.createalias("professor.students","student");
    criteria.createalias("student.assigments","assigment");
    criteria.add(Restrictions.eqProperty("professor.id","student.profid"));
    criteria.add(Restrictions.eqProperty("assigment.studentid","student.profid"));
    criteria.add(Restrictions.eq("id",2411));
return criteria.list();

原文地址:https://www.jb51.cc/java/437330.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐