Hibernation Criteria Group Results by Date from Timestamp

I have a table with a timestamp column and want to create a Hibernate criteria prediction to group results by date only from that timestamp column (results in database: YYYY-MM-DD HH: mm: ss). I know what I should be using Projection.sqlGroupProjection

but not sure if this is that hot.

Also, is it possible to write the same projection for MySQL and Oracle?

+3


source to share


2 answers


You can try using the function date

internally sqlGroupProjection

:



Projections.sqlGroupProjection("date(timestampField) as myDate", "myDate", new String[] { "myDate" }, new Type[] { StandardBasicTypes.DATE })

      

+7


source


Also, if you need to order this column, you need to create an alias for that column

Projections.alias(Projections.sqlGroupProjection(
    "trunc(timestampField) as myDate", "myDate", 
            new String[] { "myDate" }, 
            new Type[] { StandardBasicTypes.DATE }), "myDateOrder"))

      

Then you can use:



criteria.addOrder(Order.asc("myDateOrder"));

      

Note. My answer is based on nuno's answer adding ordering capability.

+4


source







All Articles