Java 使用Hibernate中的条件按月分组

Java 使用Hibernate中的条件按月分组,java,hibernate,hql,criteria,Java,Hibernate,Hql,Criteria,我正在尝试使用Criteria和ProjectionList获取一个报告,而我对通过hibernate使用它是相当陌生的。 所以我有这个模型: private Long _userId; private Category _category; private Long _companyId; private Double _amount; private Date _date; 我使用以下方法构建查询: public List sumPaymentsByUserCatego

我正在尝试使用Criteria和ProjectionList获取一个报告,而我对通过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();

 }
public List sumPaymentsByUserCategoryPeriod(类别,长用户ID,整数周期){
GregorianCalendar from=新的GregorianCalendar();
from.add(日历.月份,-期间);
List resultDTO=new ArrayList();
Criteria=getSession().createCriteria(Payment.class);
标准。添加(限制。等式(“类别”),类别);
添加(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”);
标准:setProjection(投影列表);
返回条件。list();
}
基本上,这个方法接收一个类别和一个用户ID来过滤付款记录和一个期间,由谁来指示从现在到现在我要加多少个月。如何获得按月份分组的总和结果


任何帮助或小费我都会感激的

我找到了答案,答案很简单。我为此更改了ProjectOnlist条件中的“groupProperty”:

projectionList.add(Projections.sqlGroupProjection(
    "month({alias}.DATE) as month, year({alias}.DATE) as year", 
    "month({alias}.DATE), year({alias}.DATE)", 
    new String[]{"month","year"}, 
    new Type[] {Hibernate.DOUBLE}));
好的。我将解释sqlGroupProjection。第一个参数是“select”后面的查询部分,例如:

Select [firstPartOfSqlGroupProjection] * boo;
“{alias}”是休眠在查询中用来引用表的别名


sqlGroupProjection函数的第二个参数是group by条件,第三个参数是从group by获得的列名,最后是要使用的数据类型。

您可以在hibernate中使用SQLQuery,下面是一个示例:

 public List<MonthlyPoint> groupByMonth(ChartRequest request){

   SQLQuery query = getSession().createSQLQuery("SELECT \n" +
        "sum(this_.amount) as amount, \n" +
        "extract(month from this_.fecha) as month, \n" +
        "extract(year from this_.fecha) as  year\n" +
        "FROM jornada this_ \n" +
        "GROUP BY \n" +
        "month, \n" +
        "year \n" +
        "ORDER BY \n" +
        "year asc, \n" +
        "month asc");

      query.setResultTransformer(Transformers.aliasToBean(MonthlyPoint.class));
      return query.list();
}


public class MonthlyPoint {
    private Double year;
    private Double month;
    private Double amount;
  //-- getters and setters here --
}
公共列表groupByMonth(ChartRequest请求){
SQLQuery=getSession().createSQLQuery(“选择\n”+
将(此金额)合计为金额,\n+
提取(从该\u.fecha中提取的月份)为月份,\n+
“提取(来自此\u.fecha的年份)为年份\n”+
“从jornada这里\un”+
“分组依据\n”+
“月份,\n”+
“年份\n”+
“订购人\n”+
年份asc,\n+
“月asc”);
setResultTransformer(Transformers.aliasToBean(MonthlyPoint.class));
返回query.list();
}
公共类月分{
私人双年制;
私人双月;
私人双倍金额;
//--这里有接球手和接球手--
}

如何处理不同的数据库?比如MSSQL和Oracle?由于Oracle使用_char和mssql使用weeknum…我在结果集中遇到了获取年份的问题。即使查询正确,结果集也没有年份值。通过使用帖子中的最后一个答案进行修复