我有以下功能:
/**
* Finds all entities of a certain type
* @param <T> The type of the entity
* @param entityType The class of the entity
* @return A list of all the entities found, null if the entity is not in the database
* or on error
*/
public <T> List<T> findAll(Class entityType)
{
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityType));
return getEntityManager().createQuery(cq).getResultList();
}
你会注意到这是相当重复的。无论如何我可以重构这个函数,以便它不需要将 Class 作为参数。无论如何我可以使用传入的泛型类型吗?