简体   繁体   English

Hibernate 标准:为什么没有 Subqueries.isNull(DetachedCriteria)?

[英]Hibernate criteria: why no Subqueries.isNull(DetachedCriteria)?

I want to translate a script like this into criteria:我想将这样的脚本翻译成标准:

SELECT ...
FROM A
WHERE 
  A.some_date > (select bdate from B where ...)
  OR (select bdate from B where ...) IS NULL

So, an A should be returned if either A.Some_date > B.bdate or if B.bdate is NULL.因此,如果 A.Some_date > B.bdate 或 B.bdate 为 NULL,则应返回 A。

I was expecting there to be a Subqueries.notNull(DetachedCriteria) (like there is a SubQueries.notExists(DetachedCriteria) ) but this method does not exist nor did I find something else to pull this off.我期待有一个 Subqueries.notNull(DetachedCriteria Subqueries.notNull(DetachedCriteria) (就像有一个SubQueries.notExists(DetachedCriteria) )但是这种方法不存在,我也没有找到其他方法来解决这个问题。

I could of course work around this by returning a count and check if this is > 0 or such but then I need to write 2 identical (except for the Projection) DetachedCriteria's.我当然可以通过返回一个计数来解决这个问题,并检查它是否 > 0 或这样,但是我需要写 2 个相同的(除了投影)DetachedCriteria's。

Does anyone know if/how to have the is NULL check for the above case or why this isn't provided in the Hibernate criteria API?有谁知道是否/如何拥有 NULL 检查上述情况或为什么在 Hibernate 标准 API 中没有提供? Perhaps there's a good reason...也许有一个很好的理由...

I think我认为

Subqueries.eq(null, yourDetachedCriteria)

should work.应该管用。

class MySubqueries: class 我的子查询:

public class MySubqueries {
    public static Criterion isNull(DetachedCriteria dc) {
        return new IsNullSubqueryExpression(null, null, dc);
    }
}

class IsNullSubqueryExpression: class IsNullSubqueryExpression:

public class IsNullSubqueryExpression extends SubqueryExpression {

    protected IsNullSubqueryExpression(String op, String quantifier, DetachedCriteria dc) {
        super(op, quantifier, dc);
    }

    @Override
    protected String toLeftSqlString(Criteria criteria, CriteriaQuery criteriaQuery) {
        return "";
    }

    @Override
    public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
        return super.toSqlString(criteria, criteriaQuery) + " IS NULL";
    }
}

use:利用:

detachedCriteria.add(MySubqueries.isNull(subDetachedCriteria))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM