【问题标题】:hibernate criteria aggregation on specific date在特定日期休眠标准聚合
【发布时间】:2015-01-05 01:22:35
【问题描述】:

如何将此查询转换为休眠条件:

Select cat, sum(amount) 
from transaction 
where customer =: customer 
  and month =: month 
  and year = year 
group by cat; 

我目前的标准是:

 Select cat, sum(amount) 
 from transaction 
 where customer =: customer 
 group by cat; 

Criteria cx = getCriteria()
         .setProjection(Projections.projectionList()
                .add(Projections.sum("amount", "amount")
                .add(Projections.groupProperty("cat", "cat")
                )
        .add(Restrictions.eq("customer", customer))
                ;

我尝试了很多方法,例如 .add(Restrictions.eq("month(createOn)", month) 但我收到了这个错误:

org.hibernate.QueryException: could not resolve property: month(createOn) of: com.transaction

我已确保我有数据类型为日期的 createOn 字段,并且没有语法错误。

【问题讨论】:

    标签: hibernate


    【解决方案1】:

    使用 Sql 限制 Restrictions.sqlRestriction

    Criteria cx = getCriteria()
         .setProjection(Projections.projectionList()
                .add(Projections.sum("amount", "amount")
                .add(Projections.groupProperty("cat", "cat")
                )
        .add(Restrictions.eq("customer", customer))
        .add(Restrictions.sqlRestriction("YEAR(DB_DATE_COLUMN) >= ? ", year,Hibernate.INTEGER)
        .add(Restrictions.sqlRestriction("MONTH(DB_DATE_COLUMN) >= ? ", month,Hibernate.INTEGER)   ;
    

    【讨论】:

      猜你喜欢
      • 2013-02-16
      • 2012-01-18
      • 1970-01-01
      • 2014-12-12
      • 1970-01-01
      • 2011-10-09
      • 2012-12-24
      相关资源
      最近更新 更多