任务是按特定的日期对事件进行分组,为此,我必须将日期从日期中撤出并按日期对记录进行分组。 我们曾经使用MySql,并且此代码可以正常工作:
class B : public A
{
using Base = A;
//...
}
但是切换到Postgres后,这种方法不起作用。 我需要发送类似的内容
public static Specification<AllFile> findByGroup(FileFilter fileFilter) {
return (Specification<AllFile>) (root, query, cb) -> {
final Collection<Predicate> predicates = new ArrayList<>();
if (fileFilter.getStart() != null && fileFilter.getEnd() != null) {
Date date = new Date(fileFilter.getStart().getTime());
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.setDate(1);
predicates.add(cb.greaterThanOrEqualTo(root.get("created"), date));
predicates.add(cb.lessThanOrEqualTo(root.get("created"), fileFilter.getEnd()));
}
else if(fileFilter.getStart() != null) {
Date date = new Date(fileFilter.getStart().getTime());
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.setDate(1);
Date newDate = DateUtils.addMonths(date, 1);
predicates.add(cb.greaterThanOrEqualTo(root.get("created"), date));
predicates.add(cb.lessThanOrEqualTo(root.get("created"), newDate));
}
if (fileFilter.getRoute() != null) {
predicates.add(cb.equal(root.get("route"), fileFilter.getRoute()));
}
if (fileFilter.getDevice() != null) {
predicates.add(cb.equal(root.get("device"), fileFilter.getDevice()));
}
if (fileFilter.getType() != null) {
predicates.add(cb.equal(root.get("type"), fileFilter.getType()));
}
if (fileFilter.getUser() != null) {
predicates.add(cb.equal(root.get("user"), fileFilter.getUser()));
}
if (fileFilter.getDeleted() != null) {
predicates.add(cb.equal(root.get("deleted"), fileFilter.getDeleted()));
}
query.groupBy(cb.function("day", Date.class, root.get("created")));
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
};
}
代替此
select extract(day from allfile0_.created) as dd
from all_file allfile0_
where allfile0_.created>='Tue Oct 01 00:00:00 MSK 2019' and allfile0_.created<='Fri Nov 01 00:00:00 MSK 2019'
group by dd
是否可以使用Criteria API获得我想要的东西?寻求有关自定义选择的建议,以仅选择我需要的东西。
答案 0 :(得分:0)
更改
query.groupBy(cb.function("day", Date.class, root.get("created")));
到
query = cb.createTupleQuery();
root = query.from(AllFile.class);
query.select(root.get("created")).
groupBy(cb.function("day", Integer.class, root.get("created")));
此后,代码将按预期工作