Java 코드에서 db4o의 고유 작업을 사용하는 방법을 알 수있는 사람. Java에서 예제를 찾을 수 없습니다.Java에서 데이터베이스 DB4O에서 고유 한 사용 방법은 무엇입니까?
감사합니다.
Java 코드에서 db4o의 고유 작업을 사용하는 방법을 알 수있는 사람. Java에서 예제를 찾을 수 없습니다.Java에서 데이터베이스 DB4O에서 고유 한 사용 방법은 무엇입니까?
감사합니다.
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);
나는 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
의 용기를 사용할 수 있습니다.