Reputation: 14731
I have the following Criteria API code which returns List.
I would like to convert this to List<myClass>
How can I do this?
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();
Ideally I would like to
List<Employee> emp = tuple
However the above resulted in incompatible type error, I do not know how could I cast this.
Upvotes: 3
Views: 12193
Reputation: 3386
If you insist on using a tuple query you'll have to convert the instances manually.
If myClass
is an entity you should use CriteriaQuery< myClass>
as perissf suggested, otherwise you may use a "constructor expression" to create instances directly from a select, for example:
select new com...myClass(c.id, c.price) FROM ProductList c WHERE c.....;
See this answer for an example to create such a query using the Criteria API.
Upvotes: 4