How do i group by date only from date time field in JPQL

前端 未结 4 1852
无人共我
无人共我 2021-01-12 16:11

For mysql I wrote a query like

SELECT * FROM mytable GROUP BY DATE(dateTimeField)

But i can\'t use the DATE() function in JPQL

4条回答
  •  [愿得一人]
    2021-01-12 17:01

    If You use Hibernate under the hood you can try :

    1. Create your own dialect with custom function

      public class CustomPostgresqlDialect extends PostgreSQLDialect {   
          public CustomPostgresqlDialect() {
              super();
              registerFunction("truncate_date",
              new SQLFunctionTemplate( StandardBasicTypes.TIMESTAMP, "DATE(?1)" ));
          }
      }
      
    2. Register Your dialect in persistence.xml

      
      
    3. Use your function in JPQL.

      select p from Post p group by truncate_date(p.dateCreated)
      

提交回复
热议问题