Create a JPA Criteria fully dynamically

后端 未结 1 1393
闹比i
闹比i 2021-01-18 21:11

Usually I\'m a Hibernate user and for my new project we use JPA 2.0.

My DAO receives a Container with a generic.

public class Container {
          


        
相关标签:
1条回答
  • 2021-01-18 21:26

    As long as your T is Comparable (it's required for greaterThan), you should be able to do something like the following:

    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);
            } 
            ...
        }
        ...
    }
    
    0 讨论(0)
提交回复
热议问题