gpt4 book ai didi

java - Spring Data JPA 规范 groupBy

转载 作者:太空宇宙 更新时间:2023-11-04 12:45:39 25 4
gpt4 key购买 nike

对不起,我的英语首先。

我想使用jpa进行groupby,例如:通过scrip、dstip从数据流组中选择scrip、dustup、count(*)。所以,编写这些代码:

public class DataflowSpec {
public static Specification<Dataflow> search(final String[] group, final String[] sort, final String[] desc) {
return new Specification<Dataflow>() {
@Override
public Predicate toPredicate(Root<Dataflow> root1, CriteriaQuery<?> query1, CriteriaBuilder builder) {
// TODO Auto-generated method stub

CriteriaQuery<Tuple> query = builder.createQuery(Tuple.class);

Root<Dataflow> root = query.from(Dataflow.class);


query.multiselect(root.get("srcip"), root.get("dstip"), builder.count(root));

query.groupBy(root.get("srcip"), root.get("dstip"));

query.orderBy(builder.desc(root.get("srcip").as(BigInteger.class)));
return query.getRestriction();
}
};
}
}

但是,SQL日志是: hibernate :

select
count(dataflow0_.id) as col_0_0_
from
Dataflow dataflow0_

hibernate :

select
dataflow0_.id as id1_2_,
dataflow0_.byteall as byteall2_2_,
dataflow0_.bytedn as bytedn3_2_,
dataflow0_.byteup as byteup4_2_,
dataflow0_.dstip as dstip5_2_,
dataflow0_.dstport as dstport6_2_,
dataflow0_.engieid as engieid7_2_,
dataflow0_.flag as flag8_2_,
dataflow0_.netid as netid9_2_,
dataflow0_.pkgall as pkgall10_2_,
dataflow0_.pkgdn as pkgdn11_2_,
dataflow0_.pkgup as pkgup12_2_,
dataflow0_.protocolid as protoco17_2_,
dataflow0_.rtt as rtt13_2_,
dataflow0_.srcip as srcip14_2_,
dataflow0_.srcport as srcport15_2_,
dataflow0_.updatetime as updatet16_2_
from
Dataflow dataflow0_ limit ?

那么,如何解决呢?谢谢!

最佳答案

对于仍在寻找如何在 Spring jpa 规范中应用“group by”的人们,您可以使用类似以下代码片段的内容:


...
private Dataflow dataflowFilter;

@Override
public Predicate toPredicate(Root<Dataflow> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
Predicate predicate = cb.conjunction();
predicate.getExpressions().add(cb.equal(root.get("id"), dataflowFilter.getId()));
...
<b>cq.groupBy(root.get("id"));</b>
...
return predicate;
}

关于java - Spring Data JPA 规范 groupBy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36369284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com