嗨我刚接触java并且我试图创建一个通用方法来执行具有动态条件的视图和接收对象名称(视图),条件和顺序的视图
我找到了标准api,但你总是需要发送课程
这是我试图实施但不起作用的例子
protected <E extends Object> List<E> listarPorCriteria(String nombreCampo, String valor, String criterio , Class<E> tipoRetorno) throws Exception {
CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<tipoRetorno> criteriaQuery = criteriaBuilder.createQuery(tipoRetorno.class);
Root<tipoRetorno> from = criteriaQuery.from(tipoRetorno.class);
List<T> criteria = new ArrayList<>();
if (criterio != null && !criterio.isEmpty()) {
if (criterio == CriteriosBase.IGUAL) {
criteria.add((T) criteriaBuilder.equal(from.get("nombreCampo"), valor));
criteria.add(criteriaBuilder.like(from.get("nombreCampo"), valor));
}
}
criteriaQuery.where(criteriaBuilder.and(criteria.toArray(new criteria[0])));
Query q = getEntityManager().createQuery(criteriaQuery);
return q.getResultList();
}