0

I'm getting compile time error saying Illegal class literal for the type parameter T.

public interface Dao<T, K> {
    
    default T selectById(K id) {
        EntityManager entityManager = PersistenceConnection.getInstance().createEntityManager();
        T record = entityManager.find(T.class, id);
        entityManager.close();
        return record;
    }

}

How to solve this?

Bu Saeed
  • 943
  • 1
  • 13
  • 24
  • 1
    The `thing.class` construct is a static reference that resolves at compile time. What you're trying to do is resolve it at runtime. Your implementor class for `Dao` should keep a reference to the class type, so you can use it in `entityManager.find`. Also creating a new EntityManager for each lookup is horribly wasteful. It should be one per user request. – coladict Aug 10 '21 at 08:12

0 Answers0