2012-06-19 4 views
0

IConditionTree 표현을 Criterion으로 변환해야합니다.최대 절전 모드 비어 있지 않음 표현

현재 논리 연산자 AND 및 OR을 구현했으며 이제는 NOT을 추가해야합니다. 그러나이 유형의 기준은 정션으로 표시되지 않으며 Restrictions.not (기준 기준)을 통해 생성해야합니다. 조건 트리를 처리하는 동안 입력 매개 변수로 삽입해야하는 조건을 모릅니다.

//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 연산자처럼 NOT 논리 연산자의 동일한 동작을 원한다면 어떻게해야합니까?

답변

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

내가 Restrictions.not에서 오는 분기점의 호환성과 기준에 대해 확실하지 않다. 그러나 그것은 적어도 어느 방향으로 나아가는 데 도움이됩니다. 고마워 –