我有以下Criteria API代码,它返回List。
我想将其转换为List<myClass>
我该怎么做?
CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<ProductCatalogue> pc = cq.from(ProductCatalogue.class);
Root<ProductList> al = cq.from(ProductList.class);
.......
.......
.......
Predicate[] predicates = new Predicate[predicateList.size()];
predicateList.toArray(predicates);
criteriaQuery.where(predicates);
TypedQuery<Tuple> typedQuery = getEntityManager().cq(criteriaQuery);
List<Tuple> tuple = typedQuery.getResultList();
理想情况下我想
List<Employee> emp = tuple
但是上面导致了不兼容的类型错误,我不知道怎么能投这个。
答案 0 :(得分:3)
如果您坚持使用元组查询,则必须手动转换实例
如果myClass
是一个实体,您应该使用CriteriaQuery< myClass>
作为perissf建议,否则您可以使用“构造函数表达式”直接从select创建实例,例如:
select new com...myClass(c.id, c.price) FROM ProductList c WHERE c.....;
有关使用Criteria API创建此类查询的示例,请参阅this answer。