I have the following function:
/**
* 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();
}
You will note it is rather repetitive. Is there anyway that I can refactor this function so that it does not need to take a Class as a parameter. Is there anyway that I can use the generic type being passed in?
List, I create it like sonew ArrayList<String>(). I dont create it like thisnew ArrayList<String>(String.getClass())