You should probably declare an interface (with better name):
public interface Codeable {
public String getCode();
}
And then declare the method as such:
public List<E implements Codeable> findByCode(String code, Class<E> clazz) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<E> cq = cb.createQuery(entityClass);
Root<E> root = cq.from(entityClass);
//here is my problem: how to check if there is a "code"?
// Most classes have one, but not all.
Predicate predicate = cb.equal(root.get(PersonEntity.getCode()), code);
cq.where(predicate);
TypedQuery<E> query = entityManager.createQuery(cq);
List<E> list = new ArrayList<>();
return query.getResultList();
}
You pass to the parameter clazz
you class type (in order for the compiler to know which type to actually use in the query and which type to return):
List<PersonEntity> persons = dao.findByCode("someCode", PersonEntity.getClass());
P.S
I also changed the .code
to .getCode()
in order to comply with the interface.