2012-06-19 2 views
0

Мне нужно преобразовать свое представление IConditionTree в Criterion.Hibernate empty NOT выражение

В настоящее время я реализовал логический оператор AND и OR, и теперь мне нужно добавить также NOT. Но этот тип критерия не представлен как Junction и должен быть создан через Restrictions.not (Criterion criterion). Во время обработки моего дерева условий я не знаю следующий критерий, который должен быть вставлен в качестве входного параметра.

//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 логического операнда, как и для операторов AND/OR?

ответ

0

я полагаю, не будет иметь только один ребенок в дереве, так вот он идет:

//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(); 
} 
+0

Я не уверен, о совместимости Junction и критерия, который исходит от Restrictions.not. Но это помогает мне взять хотя бы какое-то направление. Спасибо –

Смежные вопросы