繁体   English   中英

Java.Util.Date 查询问题 JPA 和 Hibernates 绑定参数

[英]Java.Util.Date Query Problem with JPA and Hibernates Binding Parameter

我目前正在使用 Springboot。 现在我想通过 JpaRepository 按 Java.util.date 查询数据,我的界面中有以下代码。

List<MyEnity> findByDate(Date date);

我使用 Java.util.date 并且我有个人原因无法更改为 java.time。 我已确保日期字段中的 MyEntity Class 具有像这样的 TemporalType.DATE

@Temporal(TemporalType.DATE) private Date date;

MySQL 中的日期字段也是日期类型

在此处输入图像描述我尝试使用上面的findByDate(Date date)方法,该方法中的日期参数显然存在于我的数据库中,但我总是得到一个空列表...

其他方法如findByName(String name)findAll()工作得很好。

我试图从 hibernate 记录 SQL 语句,我发现绑定参数 [DATE] 可能采用不同的格式? 在我的数据库中是'yyyy-mm-dd'

这是我用 id 和日期查询的日志。 在日志的底部,我注意到 Id 与查询语句绑定,但日期不会

    2563-04-10 12:18:54.649 [restartedMain] INFO  o.s.b.d.a.ConditionEvaluationDeltaLoggingListener - Condition evaluation unchanged
2563-04-10 12:19:03.241 [http-nio-8080-exec-2] INFO  o.a.c.c.C.[.[.[/api/productionplan] - Initializing Spring DispatcherServlet 'dispatcherServlet'
2563-04-10 12:19:03.282 [http-nio-8080-exec-2] INFO  o.s.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
2563-04-10 12:19:03.307 [http-nio-8080-exec-2] INFO  o.s.web.servlet.DispatcherServlet - Completed initialization in 8 ms
2563-04-10 12:19:03.346 [http-nio-8080-exec-2] DEBUG org.hibernate.SQL - 
    select
        planibtout0_.planibtoutid as planibto1_23_,
        planibtout0_.ibtqty as ibtqty2_23_,
        planibtout0_.finisheddate as finished3_23_,
        planibtout0_.finishedshiftid as finished4_23_,
        planibtout0_.itemid as itemid6_23_,
        planibtout0_.itemclassid as itemclas5_23_,
        planibtout0_.planfinishedgoodid as planfini7_23_,
        planibtout0_.planibtoutlotid as planibto8_23_,
        planibtout0_.planqty as planqty9_23_,
        planibtout0_.producerstoreid as produce10_23_,
        planibtout0_.sellerstoreid as sellers11_23_,
        planibtout0_.updateat as updatea12_23_,
        planibtout0_.updateby as updateb13_23_
    from
        planibtout planibtout0_
    where
        planibtout0_.producerstoreid=1117
        and planibtout0_.finisheddate=?
2563-04-10 12:19:03.359 [http-nio-8080-exec-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [DATE] - [Tue Apr 15 00:00:00 ICT 1477]
2563-04-10 12:19:03.363 [http-nio-8080-exec-2] INFO  c.n.t.t.s.c.SellerStoreController - -Data Not Found- No record found in database

这是 my.yml 属性

    server:
  port: 8080
  servlet:
    context-path: /api/xxx/

spring:
    datasource:
      url: jdbc:mysql://localhost:3306/xxx?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
      driverClassName: com.mysql.cj.jdbc.Driver
      username: xxx
      password: 
      continueOnError: false
      maximum-pool-size: 20
      minimum-idle: 0
      idle-timeout: 10000
      connection-timeout: 10000
      max-lifetime: 10000
      auto-commit: true

    jpa:
      show-sql: false
      hibernate:
        ddlAuto: none
      properties:
        hibernate:
          dialect: org.hibernate.dialect.MySQLDialect
          format_sql: true

startDayInWeek: 2

logging:
  level:
    com.zaxxer.hikari.HikariConfig: DEBUG
    com.ntt.th: DEBUG
  pattern:
    console: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"

实际上, Date绑定没有问题,为什么查询可以完美执行。 问题可能是应用程序时区和数据库时区不匹配。

您正在使用 IndoChina Timezone(ICT) Tue Apr 15 00:00:00 ICT 1477发送DATE ,但您正在使用serverTimezone=UTC (在 JDBC 网址中)作为数据库,这意味着您正在使用 UTC 时区作为数据库。

因此,您可以使用serverTimezone=ICT更改数据库的时区以使用 IndoChina Timezone(ICT)

暂无
暂无

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

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