2013-09-07 3 views
0

중복 문자열 삽입을 방지하기 위해 JDO 쿼리를 사용하여 기존 문자열을 확인하려고합니다.값에 "쉼표"가 포함되어 있으면 JDO 쿼리의 문자열 비교가 실패합니다.

비교할 두 문자열에 쉼표가없는 한 기존 문자열을 확인하는 내 쿼리가 올바르게 작동합니다. 쉼표가 있으면 "=="를 사용하여 비교 폭탄을 만듭니다.

예를 들어, 내가 "아키텍처"가 존재하는지 확인하기 위해 쿼리하면 올바른 결과를 얻습니다 (Horrray!). "Architecture, Engineering, and Drafting"이 존재하는지 확인하려고 시도하면 쿼리가 다시 돌아오고 동일한 값이 존재하지 않는다고 말합니다 (부!).

는 다음과 내가 사용하는 코드이기 때문에 다음 RPC에서 호출

public void addCommas() 
{ 
    final Industry e = new Industry(); 
    e.setIndustryName("Architecture, Engineering, and Drafting"); 
    persist(e); 
} 

public void addNoCommas() 
{ 
    final Industry e = new Industry(); 
    e.setIndustryName("Architecture"); 
    persist(e); 
} 

가 작동

private void persist(Industry industry) 
{ 
    if (industryNameExists(industry.getIndustryName())) 
    { 
     return; 
    } 
    final PersistenceManager pm = PMF.get().getPersistenceManager(); 
    pm.currentTransaction().begin(); 
    try 
    { 
     pm.makePersistent(industry); 
     pm.flush(); 
     pm.currentTransaction().commit(); 
    } catch (final Exception ex) 
    { 
     throw new RuntimeException(ex); 
    } finally 
    { 
     if (pm.currentTransaction().isActive()) 
     { 
      pm.currentTransaction().rollback(); 
     } 
    pm.close(); 
    } 
} 

쿼리

을 지속 6,
public static boolean industryNameExists(final String industryName) 
{ 
    final PersistenceManager pm = PMF.get().getPersistenceManager(); 
    Query q = null; 
    q = pm.newQuery(Industry.class); 
    q.setFilter("industryName == industryNameParam"); 
    q.declareParameters(String.class.getName() + " industryNameParam"); 
    final List<Industry> industry = (List<Industry>) q.execute(industryName.getBytes()); 
    boolean exists = !industry.isEmpty(); 
    if (q != null) 
    { 
     q.closeAll(); 
    } 
    pm.close(); 
    return exists; 
} 

은 JDO 엔티티는

@PersistenceCapable(detachable = "true") 
public class Industry implements StoreCallback 
{ 
    @NotNull(message = "Industry Name is required.") 
    private String   industryName; 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    @PrimaryKey 
    private Key    key; 

    public Industry() 
    { 
     super(); 
    } 

    public Key getIndustryKey() 
    { 
     return key; 
    } 

    public String getIndustryName() 
    { 
     return industryName; 
    } 

    @Override 
    public void jdoPreStore() 
    { 
     if (industryName != null) 
     { 
      industryName = industryName.trim(); 
     } 
    } 
    public void setIndustryName(final String industryName) 
    { 
     this.industryName = industryName; 
    } 
} 

해상도 나 감독을 정확히 파악에 어떤 생각이 아주 많이 주시면 감사하겠습니다. Cheerio.

+0

검색어가 약간 이상합니다. JDO 클래스 및 매핑을 게시 할 수 있습니까? – TheArchitect

+0

문제의 JDO 파일을 추가했습니다. 나는 간결함을 위해 getters/setters 및 속성 중 일부를 제거했습니다. 나는 쿼리를 다시 작성하는 방법에 대한 제안을 받는다. –

+0

나는 여전히 쉼표로 구분 된 문자열에 혼란스러워합니다. industryName 매개 변수의 입력입니까? 또는 Industry.industryName 값? 전자가 단일 메소드 호출에 쉼표로 분리 된 문자열을 입력으로 전달한다면? 또한 industryNameExists에 전화를 게시 할 수 있습니까? 도움이 될 것입니다. – TheArchitect

답변

0

따라서 industryNameExists ("Architecture, Engineering, and Drafting")를 호출하고 industryName이 "Architecture, Engineering, Drafting"과 정확히 일치하도록 JDO를 일치 시키려고합니까?

오타 또는 공백이 없으면 getBytes() 만 의심됩니다. 다음보십시오 : "(: industryNameParam) this.industryName.equalsIgnoreCase"와 "this.industryName.startWith (: industryNameParam)"

Query q = pm.newQuery(Industry.class, "this.industryName == :industryNameParam"); 
List<Industry> industry = (List<Industry>) q.execute(industryName); 

또한 같은 변화 필터를 시도 할 수 있습니다 문제를 해결합니다.

여전히 작동하지 않는 경우 검토를 위해 생성 된 SQL을 기록하고 작동하는 손으로 작성된 쿼리와 비교하십시오.

+0

제안 된 제안 (코드 블록 작동). 건배! –

관련 문제