2012-04-24 14 views
4

通常、私はHibernateユーザーです。私の新しいプロジェクトでは、JPA 2.0を使用します。完全に動的にJPA基準を作成する

私のDAOは、汎用のコンテナを受け取ります。

if (">".equals(container.getOperation()) { 
    criteriaBuilder.greaterThan(root.get(container.getFieldId()), container.getValue()); 
} 

ので私はこのようなタイプを指定する必要があります:

public class Container<T> { 
    private String fieldId; // example "id" 
    private T value;   // example new Long(100) T is a Long 
    private String operation; // example ">" 

    // getter/setter 
} 

次の行はコンパイルされません

if (">".equals(container.getOperation()) { 
    criteriaBuilder.greaterThan(root.<Long>get(container.getFieldId()), (Long)container.getValue()); 
} 

しかし、私はそれを行うにはしたくありません!私は自分のコンテナにジェネリックを使用しているからです! アイデアはありますか?限り、あなたのTComparableあるよう

答えて

4

(それはgreaterThanのために必須です)、次のような何かを行うことができるはず:

public class Container<T extends Comparable<T>> { 
    ... 
    public <R> Predicate toPredicate(CriteriaBuilder cb, Root<R> root) { 
     ... 
     if (">".equals(operation) { 
      return cb.greaterThan(root.<T>get(fieldId), value); 
     } 
     ... 
    } 
    ... 
} 
関連する問題