对不起我的英语。
我想使用jpa来分组,比如:从数据流组中选择scrip,dustup,count(*),dstip。 所以,写下这些代码:
select
count(dataflow0_.id) as col_0_0_
from
Dataflow dataflow0_
但是,SQL日志是: 休眠:
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 ?
休眠:
$result = mysql_query("SELECT $_SESSION FROM users",$db);
那么,如何解决呢?谢谢!
答案 0 :(得分:0)
规范不支持groupBy。 SimpleJpaRepository通过query.select(root)替换了query.select/multiselect
答案 1 :(得分:0)
您可以通过group by
来获得弹簧数据specification
,只需遵循
section 2.6或section 3.6(2.0之前或之后的版本)。对于单个存储库操作,两个版本具有相同的解决方案。对于* all *存储库解决方案,在2.0之前使用customized factory bean,而在2.0以后则省略该工厂bean操作。
public Map<AlarmMsg.AlarmLevel, Long> testSpecification(String neId) {
SingularAttribute attribute = AlarmData_.isClear;
Specification<Object> where = Specification.where(
(root, query, cb) -> cb.equal(root.get(attribute), false)
);
final Map<AlarmMsg.AlarmLevel, Long> result = alarmDataRepository.groupAndCount(AlarmData_.alarmLevel, where );
return result;
}
存储库:
public interface AlarmDataRepository extends JpaRepository<AlarmData, Long>, JpaSpecificationExecutor<AlarmData>, CustomizedGroupCountRepository {
片段存储库及其实现:
public interface CustomizedGroupCountRepository {
Map<AlarmMsg.AlarmLevel, Long> groupAndCount(SingularAttribute singularAttribute, Specification where);
}
public class CustomizedGroupCountRepositoryImpl implements CustomizedGroupCountRepository {
private final EntityManager entityManager;
public CustomizedGroupCountRepositoryImpl(EntityManager entityManager) {
Assert.notNull(entityManager, "EntityManager must not be null!");
this.entityManager = entityManager;
}
@Override
public Map<AlarmMsg.AlarmLevel, Long> groupAndCount(SingularAttribute singularAttribute, Specification where) {
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
final CriteriaQuery<Tuple> query = criteriaBuilder.createQuery(Tuple.class);
final Root<AlarmData> root = query.from(AlarmData.class);
final Path<AlarmMsg.AlarmLevel> expression = root.get(singularAttribute);
query.multiselect(expression, criteriaBuilder.count(root));
query.select(criteriaBuilder.tuple(expression, criteriaBuilder.count(root)));
query.where(where.toPredicate(root, query, criteriaBuilder));
query.groupBy(expression);
final List<Tuple> resultList = entityManager.createQuery(query).getResultList();
return resultList.stream()
.collect(toMap(
t -> t.get(0, AlarmMsg.AlarmLevel.class),
t -> t.get(1, Long.class))
);
}
}
“一库一库”和“一库一库”之间的主要区别在于,在“一库一库”的情况下,它可以访问真实的实体类,如春季的User
参考文件。这样一来,您就无需使用泛型类型来引用任何类型的实体,而在所有存储库的情况下,自定义方法的实现将使用泛型,并且其类信息可以(或必须)是从第3.6节所述的注入JpaEntityInformation
中获得。
答案 2 :(得分:0)
对于仍然在Spring jpa Specification中寻求如何应用“分组依据”的人们,您可以使用以下代码段:
...
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()));
...
cq.groupBy(root.get("id"));
...
return predicate;
}