2010-05-13 5 views

답변

4

Afaik, distinct-operator에는 빌드가 없습니다. 해결 방법을 사용해야합니다. 예를 들어, 고유 작업에 Set를 사용할 수 있습니다.

개체의 동일성으로 구별됩니다. 당신의 클래스가 적절한 equals()와 hashCode() - 구현을 가지고있을 때 이것은 훌륭하게 작동합니다 :

ObjectSet<TestClass> result = container.query(TestClass.class); 
// will use the equals-method of TestClass. 
Set<TestClass> distinctResult = new HashSet<TestClass>(result); 

특정 필드로 구별됩니다. 특정 필드로 구분할 때 유용합니다.

ObjectSet<TestClass> result = container.query(TestClass.class); 
Set<TestClass> distinctResultByField = new TreeSet<TestClass>(new Comparator<TestClass>() { 
    public int compare(TestClass o1, TestClass o2) { 
     return o1.getTestField().compareTo(o2.getTestField()); 
    } 
}); 
distinctResultByField.addAll(result); 
0

나는 select from ... order by... distinct 쿼리를 원했습니다. 여기에 내가 그것을 어떻게입니다 : 돌이켜에서

public static List<Country> getAllCountries(final CountryOrder order) 
{ 
    ObjectContainer oc = DbHelper.getInstance().getContainer(); 
    ObjectSet<Country> countries = oc.query(new Predicate<Country>() 
    { 
     HashSet<Country> distinctCountries = new HashSet<Country>(); 

     @Override 
     public boolean match(Country et) 
     { // Need this to make a distict query... sad 
     if (distinctCountries.contains(et)) 
     { 
      return false; 
     } 

     distinctCountries.add(et); 
     return true; 
     } 
    }, new Comparator<Country>() 
    { 
     @Override 
     public int compare(Country o1, Country o2) 
     { 
     switch (order) 
     { 
      case COUNTRY_CODE: 
       return o1.getCountryCode().compareTo(o2.getCountryCode()); 
      case COUNTRY_NAME: 
       return o1.getCountryName().compareTo(o2.getCountryName()); 
      default: 
       return o1.compareTo(o2); 
     } 
     } 
    }); 

    return countries; 
} 

, 당신은 distinctCountries을 위해 대신 HashSet의 용기를 사용할 수 있습니다.