在JPA 2中使用投影 [英] Using Projections in JPA 2
问题描述
我需要转换一个Hibernate标准查询,如下所示:
curList = session.createCriteria(Islem.class)
.createAlias(workingDay,d)
.setProjection(Projections.sum(amount))
.add(Restrictions.eq(currency,CURRENCY))
.add(Restrictions.eq(product,product))
.add(Restrictions.ne(status,INACTIVE))
.add(Restrictions.eq(d.status, ACTIVE))
.getResultList();
然而,在JPA(2)中,我不知道如何实现投影 - 在这种情况下 - 和。 Hibernate和JPA(甚至Hibernate JPA 2)在标准查询中存在巨大差异,这很奇怪。
我从
$ b $开始b
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery< Islem> cq = cb.createQuery(Islem.class);
Root< Islem> isr = cq.from(Islem.class);
cq.select(isr).where(cb.equal(isr.get(currency),CURRENCY),
cb.notEqual(isr.get(status),INACTIVE),
cb.equal(isr.get(product),product));
但是不知道如何在这里实现投影既不是别名
$ b
使用来解决这个问题是一个老问题, CriteriaBuilder
,与Hibernate不同,您总是从要查询的类型结果开始,然后构建投影。
CriteriaBuilder cb = em.getCriteriaBuilder();
//我们想要整数结果
CriteriaQuery<整数> cq = cb.createQuery(Integer.class);
//根不需要匹配查询结果的类型!
Root< Islem> isr = cq.from(Islem.class);
//创建一个连接以访问工作日的变量状态
加入< Islem,WorkingDay> join = isr.join(workingDay,JoinType.INNER);
//创建总和表达式
表达式<整数> sum = cb.sum(isr.get(amount));
$ b cq.where(
cb.equal(isr.get(currency),CURRENCY),
cb.notEqual(isr.get(status),INACTIVE ),
cb.equal(isr.get(product),product),
cb.equal(join.get(status),ACTIVE)
).select(sum );
另一方面,如果您想查询实际的金额值,您可以:
CompoundSelection< Integer> projection = cb.construct(Integer.class,cb.sum(isr.get(amount)));
cq.where(..)。select(projection);
列表<整数>金额= em.createQuery(cq).getResultList();
I need to convert a Hibernate criteria query like the following
curList = session.createCriteria(Islem.class)
.createAlias("workingDay", "d")
.setProjection(Projections.sum("amount"))
.add(Restrictions.eq("currency", CURRENCY))
.add(Restrictions.eq("product", product))
.add(Restrictions.ne("status", INACTIVE))
.add(Restrictions.eq("d.status", ACTIVE))
.getResultList();
However in JPA (2) I have no idea how to implement the projection - in this case - the sum. It's odd that Hibernate and JPA (even Hibernate JPA 2) have this tremendous differences especially in criteria queries.
I start by
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Islem> cq = cb.createQuery(Islem.class);
Root<Islem> isr = cq.from(Islem.class);
cq.select(isr).where(cb.equal(isr.get("currency"), CURRENCY),
cb.notEqual(isr.get("status"), INACTIVE),
cb.equal(isr.get("product"), product));
however have no idea how to implement the projection here neither the alias
It's an old question, but let's give an example:
With CriteriaBuilder
, unlike Hibernate, you always start off with the type of the result you want to query and then construct the projection.
CriteriaBuilder cb = em.getCriteriaBuilder();
//We want Integer result
CriteriaQuery<Integer> cq = cb.createQuery(Integer.class);
//The root does not need to match the type of the result of the query!
Root<Islem> isr = cq.from(Islem.class);
//Create a join to access the variable status of working day
Join<Islem,WorkingDay> join = isr.join("workingDay",JoinType.INNER);
//Create the sum expression
Expression<Integer> sum = cb.sum(isr.get("amount"));
cq.where(
cb.equal(isr.get("currency"), CURRENCY),
cb.notEqual(isr.get("status"), INACTIVE),
cb.equal(isr.get("product"), product),
cb.equal(join.get("status"), ACTIVE)
).select(sum);
On the other hand if you wanted to query for the actual "amount" values, you could do:
CompoundSelection<Integer> projection = cb.construct(Integer.class, cb.sum(isr.get("amount")));
cq.where(..).select(projection);
List<Integer> amounts = em.createQuery(cq).getResultList();
这篇关于在JPA 2中使用投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!