【问题标题】:Hibernate empty NOT expression休眠空 NOT 表达式
【发布时间】:2012-06-19 14:00:22
【问题描述】:

我需要将我的 IConditionTree 表示转换为 Criterion。

目前我已经实现了逻辑运算符 AND 和 OR,现在我还需要添加 NOT。但是这种类型的标准不表示为 Junction,需要通过 Restrictions.not(Criterion criteria) 创建。在处理我的条件树期间,我不知道必须作为输入参数插入的以下标准。

//initial call
criteria.add(generateNodeCriteria(conditionTree.getRoot(), conditionTree));

private Criterion generateNodeCriteria(IConditionTreeNode node, IConditionTree conditionTree) throws SomeException {
    // Create criteria for condition
    if (node.getCondition() != null) {
        return createConditionCriterion(node.getCondition());
    }

    // Create criteria for logical operator
    if (node.getLogicalOperator() != null) {
        // What logical operator to use?
        Junction junction = null;
        switch (node.getLogicalOperator()) {
        case AND:
            junction = Restrictions.conjunction();
            break;
        case OR:
            junction = Restrictions.disjunction();
            break;
        }

        // Add all direct children of logical operator into conjunction
        for (IConditionTreeNode childNode : conditionTree.getOneLevelChildren(node)) {
            junction.add(generateNodeCriteria(childNode, conditionTree));
        }
        return junction;
    }
    throw new SomeException();
}

有什么办法,如何将 NOT 逻辑运算符实现到 switch 部分?如果我希望 NOT 逻辑运算符的行为与 AND/OR 运算符相同,我应该改变什么?

【问题讨论】:

    标签: hibernate hibernate-criteria logical-operators


    【解决方案1】:

    我想你的树中只有一个孩子,所以就这样吧:

    //initial call
    criteria.add(generateNodeCriteria(conditionTree.getRoot(), conditionTree));
    
    private Criterion generateNodeCriteria(IConditionTreeNode node, IConditionTree    conditionTree) throws SomeException {
        // Create criteria for condition
        if (node.getCondition() != null) {
        return createConditionCriterion(node.getCondition());
    }
    
    // Create criteria for logical operator
    if (node.getLogicalOperator() != null) {
    
        boolean addChildren = false;
    
        // What logical operator to use?
        Junction junction = null;
        switch (node.getLogicalOperator()) {
        case AND:
            junction = Restrictions.conjunction();
            addChildren = true;
            break;
        case OR:
            junction = Restrictions.disjunction();
            addChildren = true;
            break;
        case NOT:
            junction = Restrictions.not(generateNodeCriteria(conditionTree.getOneLevelChildren(node).get(0), conditionTree));
        }
    
        if (addChildren)
          // Create all children first
          for (IConditionTreeNode childNode : conditionTree.getOneLevelChildren(node)) {
              junction.add(generateNodeCriteria(childNode, conditionTree));
          }
    
        return junction;
    }
    throw new SomeException();
    }
    

    【讨论】:

    • 我不确定来自 Restrictions.not 的 Junction 和 Criterion 的兼容性。但这至少有助于我采取一些方向。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 2015-01-06
    相关资源
    最近更新 更多