我正在尝试使用Criteria和ProjectionList获取报告,而我通过hibernate使用它是非常新的.所以我有这个模型:
private Long _userId;
private Category _category;
private Long _companyId;
private Double _amount;
private Date _date;
Run Code Online (Sandbox Code Playgroud)
我用这个构建查询:
public List sumPaymentsByUserCategoryPeriod(Category category, Long userId,Integer period){
GregorianCalendar from = new GregorianCalendar();
from.add(Calendar.MONTH, -period);
List<CategoryAmount> resultDTO= new ArrayList<CategoryAmount>();
Criteria criteria = getSession().createCriteria(Payment.class);
criteria.add(Restrictions.eq("_category", category));
criteria.add(Restrictions.eq("_userId",userId));
criteria.add(Restrictions.between("_date", from.getTime(), new Date()));
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.sum("_amount"));
projectionList.add(Projections.groupProperty("_date"));
criteria.setProjection(projectionList);
return criteria.list();
}
Run Code Online (Sandbox Code Playgroud)
基本上这种方法会收到一个Category和一个userId来过滤付款记录和一个期间,这个期间将指示从现在到我想要总和多少个月.如何按月分组总和结果?
任何帮助或提示,我会很感激!