Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JPA转换列表<;元组>;列出<;myClass>;_Jpa_Tuples_Eclipselink_Jpa 2.0_Criteria Api - Fatal编程技术网

JPA转换列表<;元组>;列出<;myClass>;

JPA转换列表<;元组>;列出<;myClass>;,jpa,tuples,eclipselink,jpa-2.0,criteria-api,Jpa,Tuples,Eclipselink,Jpa 2.0,Criteria Api,我有以下返回列表的标准API代码 我想将其转换为列表 我该怎么做 CriteriaQuery<Tuple> cq = cb.createTupleQuery(); Root<ProductCatalogue> pc = cq.from(ProductCatalogue.class); Root<ProductList> al = cq.from(ProductList.class); ....... ....... ....... Predicate[]

我有以下返回列表的标准API代码

我想将其转换为
列表

我该怎么做

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();
CriteriaQuery cq=cb.createTupleQuery();
根pc=cq.from(productcatalog.class);
根al=cq.from(ProductList.class);
.......
.......
.......
谓词[]谓词=新谓词[predicateList.size()];
谓词列表toArray(谓词);
criteriaQuery.where(谓词);
TypedQuery TypedQuery=getEntityManager().cq(criteriaQuery);
List tuple=typedQuery.getResultList();
理想情况下,我想

List<Employee> emp = tuple
List emp=tuple

但是,上述操作导致了不兼容的类型错误,我不知道如何转换此错误。

如果坚持使用元组查询,则必须手动转换实例。
如果
myClass
是一个实体,则应按照perissf的建议使用
CriteriaQuery
,否则可以使用“构造函数表达式”直接从select创建实例,例如:

select new com...myClass(c.id, c.price) FROM ProductList c WHERE c.....;

有关使用Criteria API创建此类查询的示例,请参见。

您真的需要元组吗?如果没有,为什么不创建一个直接的
标准查询
?有关实例,请参阅使用元组的原因是。为了摆脱
不允许维护缓存或编辑部分对象查询
我已经开始使用Tuple。我知道,您不想选择实体所需的所有数据,这就是为什么JPA无法从(部分)查询创建实例。所以您使用的是元组查询,很好。但是,如何在任意元组和类之间实现自动映射呢?魔术如果你想的好的话,我可以向查询传递某种映射指令,瞧,这就是我在回答中提到的所谓的“构造函数表达式”!查询之后,您又回到了普通的Java世界,这意味着元组和类之间不可能存在任何类型的自动映射(cast)!但是使用Java8流,您的转换可能会像返回tuples.stream().map(t->newMyClass(t.get(idExpr),t.get(pricexpr),…).collect(Collectors.toList())一样简单,然后使用构造函数表达式或循环。如果这还不够清楚:您也可以对实体使用构造函数表达式。Robin,我已经通过构造函数表达式解决了这个问题,非常感谢您的洞察力