I use a Abstract Base class with generic implementation to access my database with JPA. I also use the Entity Meta Model.
public List<PersonEntity> findByCode(String code) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<PersonEntity> cq = cb.createQuery(PersonEntity.class);
Root<PersonEntity> root = cq.from(PersonEntity.class);
Predicate predicate = cb.equal(root.get(PersonEntity_.code), code);
cq.where(predicate);
TypedQuery<PersonEntity> query = entityManager.createQuery(cq);
List<PersonEntity> list = new ArrayList<>();
return query.getResultList();
}
I want to move this to a generic base class, since this peace of code is use a lot of times. How do I check if there is a "code"? Not all classes have one.
public List<E> findByCode(String code) {
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_.code), code);
cq.where(predicate);
TypedQuery<E> query = entityManager.createQuery(cq);
List<E> list = new ArrayList<>();
return query.getResultList();
}