将类型作为参数传递给api
答案 0 :(得分:1)
您可以将Class作为参数,如下所示,
buildCriteriaQ(Class<?> parentClass) {
CriteriaBuilder cb = factory.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(entityClass);
Root<?> a = cq.from(entityClass);
cq.select(a);
Query query = factory.createEntityManager().createQuery(cq);
List results = query.getResultList();
System.out.println(results);
}
并按如下所示调用此方法,
buildCriteriaQ(Item.class);
答案 1 :(得分:0)
您可以尝试使用 reflection 来创建Class
类型的对象的实例
例如
Class clazz = ... //your class
Constructor constructor = clazz.getConstructor(prepareParameterTypes(parameters));
constructor.newInstance(parameters);