我試圖使用Criteria和ProjectionList得到一個報告,而且我很新,通過hibernate使用它。 所以我有這個模型:在Hibernate中按標準分組
private Long _userId;
private Category _category;
private Long _companyId;
private Double _amount;
private Date _date;
而且我用這個構建查詢:
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();
}
基本上這個方法接收類別和用戶ID來過濾付款記錄和一段時間,誰指示我想從現在到後退幾個月。我怎樣才能得到按月分組的總和結果?
任何幫助或提示我會感激它!
如何處理不同的數據庫?如MSSQL和Oracle? 由於Oracle使用to_char和mssql使用weeknum ... – zolibra 2015-05-06 06:01:43