2012-09-29 2 views
1

나는 자바 코드의 덩어리를 가지고있는 하드 코드이는 프로그래밍 방식

session = HibernateUtils.beginTransaction("outpatient"); 
     Criteria criteria = session.createCriteria(AugmentToken.class); 
     session.beginTransaction(); 
     if (type == Constants.ICD9CPT) { 
      criteria.add(Restrictions.disjunction() 
        .add(Restrictions.eq("codeType", "d")) 
        .add(Restrictions.eq("codeType", "p")) 
        .add(Restrictions.eq("codeType", "c"))); 
     } else if (type == Constants.EM) { 
      criteria.add(Restrictions.disjunction() 
        .add(Restrictions.eq("codeType", "eros")) 
        .add(Restrictions.eq("codeType", "ehpi")) 
        .add(Restrictions.eq("codeType", "epe"))); 
     } 

처럼 보인다하지만 이것은 매우 우아 코드가 아닌 최대 절전 모드 분리 쿼리. 내가하고 싶은 것은 메소드에 일련의 코드 유형을 전달하고 다이내믹 기준을 동적으로 구성하는 것이다. 내가 본 모든 웹 사이트는 위와 유사한 분리형 쿼리의 예를 제공하지만 코드 유형 수가 다양 할 수 있기 때문에 조건에 대한 제한 구성을 어렵게 코딩하고 싶지 않기 때문에이 방법은 저에게는 효과적이지 않습니다.

어떻게하면됩니까?

내가이 알아 낸 생각

엘리엇

답변

2

, 감사합니다. disjunction을 변수로 만든 다음 순차적으로 변수에 추가합니다. 특히
는 :

String [] codeTypes = new String[3]; 
codeTyes[0]="d"; 
codeTypes[1]="p"; 
codetypes[2]="c"; 
/* note the above would normally be passed into the method containing the code below */ 
Criteria criteria = session.createCriteria(AugmentToken.class); 
    session.beginTransaction(); 
Disjunction disjunction = Restrictions.disjunction(); 
for (int x = 0; x < codeTypes.length; x++) { 
    disjucntion.add(Restrictions.eq("codeType",codeTypes[x]); 
} 
criteria.add(disjunction); 

나는 처음에 답이 책은 books.google.com에서 액세스 할 수있는 페이지 (214)에서 최대 절전 모드 발견했다.