gpt4 book ai didi

java - Hibernate-Criteria:在 'ON Clause' 中使用多个条件

转载 作者:行者123 更新时间:2023-12-01 15:08:59 31 4
gpt4 key购买 nike

我遇到一种情况,在使用 Hibernate Criteria 时,我需要在 ON 子句中应用另一个条件。

我正在编写 MySQL,但无法找到任何合理的方法在 Hibernate Criteria 中转换它。

SELECT s.StudentID AS StudentID, sc.CourseID AS CourseID
FROM Student s
LEFT JOIN StudentCourse sc ON
(sc.StudentID = s.StudentID AND sc.CourseID = 'XXX')

我在此查询中执行的操作

我希望所有学生列表都注册或未注册指定类(class)。

如果我有四名学生,并且只有一名学生注册了一门类(class),结果应该是这样的

StudentID, CourseID
1 NULL
2 XXX
3 NULL
4 NULL

到目前为止我所做的

Criteria cr = getSession().createCriteria(Student.class);
cr.createAlias("studentCourse", "sc", CriteriaSpecification.LEFT_JOIN)
.add(Restrictions.eq("sc.CourseID", 'XXX'));

通过运行此语句,我得到与以下查询等效的查询

SELECT *
FROM Student s
LEFT JOIN StudentCourse sc ON (sc.StudentID = s.StudentID)
WHERE sc.CourseID = 'XXX'

现在这个查询并没有达到查询的目的。它仅返回一行 CourseID 不为空的行。

编辑

hibernate 版本3.4.0GA

最佳答案

您可以将第二个条件移至通用 WHERE 子句中,例如

WHERE (sc.CourseID = 'XXX' or sc.CourseID is null)

关于java - Hibernate-Criteria:在 'ON Clause' 中使用多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12560739/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com