Hibernate无法从MySQL数据库获取给定日期的正确行

时间:2018-11-10 01:22:56

标签: mysql spring hibernate spring-data-jpa hibernate-criteria

我有一个Spring MVC项目和一个MySQL数据库,该数据库的表包含以下行:

enter image description here

我有一个函数可以在日期field之前排行:

@Override
public List listAllTimeSheetDetailsByDate(LocalDate date) {
    CriteriaBuilder cb = getSession().getCriteriaBuilder();
    CriteriaQuery<TimeSheetDetails> cr = cb.createQuery(TimeSheetDetails.class);
    Root<TimeSheetDetails> root = cr.from(TimeSheetDetails.class);
    cr.select(root).where(cb.equal(root.get("date"), date));
    Query<TimeSheetDetails> query = getSession().createQuery(cr);
    List<TimeSheetDetails> results = query.getResultList();
    return results;
}

我正在使用Hibernate的getCriteriaBuilder()createQuery()实现。

问题是,当我想检索日期为2018-11-03的行时,我会返回对应于日期2018-11-02的行。我正在使用LocalDate,所以每月的某几天不是从0开始的。这是Hibernate's Log

INFO    [default task-4] stdout  - Hibernate: select timesheetd0_.timeSheetDetailsId as timeShee1_2_, timesheetd0_.date as date2_2_, timesheetd0_.employeeId as employee3_2_, timesheetd0_.endTime as endTime4_2_, timesheetd0_.startTime as startTim5_2_ from TimeSheetDetails timesheetd0_ where timesheetd0_.date=?
**TRACE   [default task-4] org.hibernate.type.descriptor.sql.BasicBinder  - binding parameter [1] as [DATE] - [2018-11-03]**
TRACE   [default task-4] org.hibernate.type.descriptor.sql.BasicExtractor  - extracted value ([timeShee1_2_] : [INTEGER]) - [4]
**TRACE   [default task-4] org.hibernate.type.descriptor.sql.BasicExtractor  - extracted value ([date2_2_] : [DATE]) - [2018-11-02]**
TRACE   [default task-4] org.hibernate.type.descriptor.sql.BasicExtractor  - extracted value ([employee3_2_] : [VARCHAR]) - [3]
TRACE   [default task-4] org.hibernate.type.descriptor.sql.BasicExtractor  - extracted value ([endTime4_2_] : [TIME]) - [05:00]
TRACE   [default task-4] org.hibernate.type.descriptor.sql.BasicExtractor  - extracted value ([startTim5_2_] : [TIME]) - [01:00]

我不明白为什么它得到了前一个日期。此外,当我想检索"2018-11-30"日期时,我会得到一个空集。我不明白为什么会这样。

0 个答案:

没有答案