简体   繁体   English

带有 where 子句的 Group By 的休眠条件

[英]Hibernate Criteria for Group By with where where clause

I want to use hibernate criteria for group by clause with where clause.我想对带有 where 子句的 group by 子句使用休眠条件。

like if I have an sql like this:就像我有这样的 sql:

"select max(id) from Employee where age>25 group by trunc(created_date) " “选择 max(id) from Employee where age>25 group by trunc(created_date) ”

Problem is that my created_date column has time also, but I want to use group by on trucated date,excluding time.问题是我的 created_date 列也有时间,但我想在截断日期使用 group by,不包括时间。

How could I pass truncated date in hibernate Projection while using criteria group BY.如何在使用标准组 BY 时在休眠投影中传递截断日期。

    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
    String createDateInString = "26-FEB-20";

    try {

        Date createDate = formatter.parse(createDateInString);

    } catch (ParseException e) {
        e.printStackTrace();
    }
List result = session.createCriteria(Employee.class)       
                    .add(Restrictions.ge("age",new Integer(25))) 
        .add(Restrictions.eq("created_date", new java.sql.Timestamp(createDate.getTime())))
                    .setProjection(Projections.projectionList()
                            .add(Projections.groupProperty("id"))
                            .add(Projections.max("name"))
                    ).list();

I think there will be chance of error while comparing date.我认为比较日期时可能会出错。 Otherwise groupBy can be done using Projections否则 groupBy 可以使用 Projections 完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM