0

I am trying to create a generic method that defines a search. The search will receive the field name and the value to be searched as input parameters, not the object itself.

The problem is that I am getting the following errors in the 'return' line:

Multiple markers at this line
    - Type safety: The method from(Class) belongs to the raw type AbstractQuery. References to generic type AbstractQuery<T> should be parameterized
    - Type safety: The method where(Expression) belongs to the raw type CriteriaQuery. References to generic type CriteriaQuery<T> should be parameterized
    - Illegal class literal for the type parameter T
    - The method findOne(Example<S>) in the type QueryByExampleExecutor<T> is not applicable for the arguments (CriteriaQuery)
public abstract class ModelController<T extends HasId, R extends ModelRepository<T>> {

    private T _findId(String field, String needle) {
        EntityManager em;
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery cq = cb.createQuery();

        // Here I got the errors
        return repository.findOne(cq.where(cb.equal(cq.from(T.class).get(field), needle))); 
    }
    
}

Any clues?

vuco
  • 161
  • 2
  • 6
  • I don't think you can use T.class since T is a parameter and java uses Type Erasure in generics. –  Mar 13 '21 at 21:30

1 Answers1

0

You're issue lies with trying to access the class of T with T.class. Java erases type for generics which you can see in their documentation so the only way to get a class reference is to extend your method to take in a Class<T>.

Henry Twist
  • 5,666
  • 3
  • 19
  • 44