有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (4) 个答案

  1. # 2 楼答案

    如果在引擎盖下使用Hibernate,您可以尝试:

    1. 使用自定义函数创建自己的方言

      public class CustomPostgresqlDialect extends PostgreSQLDialect {   
          public CustomPostgresqlDialect() {
              super();
              registerFunction("truncate_date",
              new SQLFunctionTemplate( StandardBasicTypes.TIMESTAMP, "DATE(?1)" ));
          }
      }
      
    2. persistence.xml中注册您的方言

      <property name="hibernate.dialect" value="my.own.CustomPostgresqlDialect"/>
      
    3. 在JPQL中使用您的函数

      select p from Post p group by truncate_date(p.dateCreated)
      
  2. # 4 楼答案

    对于Hibernate:

    建议1:

    使用cast(dateTimeField as date)。见here

    建议2:

    您可以尝试连接yearmonthyearHQL expressions

    看看this testcase的第360行

    str(year(current_date))||'-'||str(month(current_date))||'-'||str(day(current_date))
    

    但是花点时间看看生成的SQL,它可能(也可能)丑陋而缓慢