LocalDate 导致数据截断:日期值不正确

LocalDate causes Data truncation: Incorrect date value

我正在尝试使用 mysql 和 jpa 构建一个应用程序来处理具有不同数据类型的用户输入。当我 运行 应用程序时,我收到 "MysqlDataTruncation: Data truncation: Incorrect date value:" 错误。我真的需要帮助来解决这个问题,我已经进行了一些研究,但我无法弄清楚。 我在 mysql 中使用 Date 类型,在我的实体 class 中使用 LocalDate 类型。可能吗?

这是我的第一个实体class:

      @Entity
      @Table(name = "flight")
      public class Flight implements Serializable {

     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
       private Integer flightId;
       private String flightName;
       private String Name;
       private LocalDate firstDate;
       private LocalDate lastDate;

预订实体class:

       @Entity
       @Table(name="booking")
       public class Booking implements Serializable {

       @Id
       @GeneratedValue(strategy = GenerationType.AUTO)
       private Integer reservationId;
       private String hotelRoomType;
       private double totalAmount;

数据库tables:

    CREATE TABLE `flight` (
    `flightId` int NOT NULL AUTO_INCREMENT,
    `flightName` varchar(50) DEFAULT NULL,
    `Name` varchar(50) DEFAULT NULL,
    `firstDate` date NOT NULL,
    `lastDate` date NOT NULL,

预订table:

 CREATE TABLE `booking` (
`reservationId` int NOT NULL,
`hotelRoomType` varchar(30) NOT NULL,
 `totalAmount` decimal(10,2) NOT NULL  

)

错误信息: 异常

        org.springframework.web.util.NestedServletException: Request processing failed; nested 
        exception is javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse 
        Persistence Services - 2.5.2.v20140319-9ad6abd): 
        org.eclipse.persistence.exceptions.DatabaseException
        Internal Exception: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: 
        Incorrect date value:
        '\xAC\xED\x00\x05sr\x00\x0Djava.time.
        Ser\x95]\x84\xBA\x1B"H\xB2\x0C\x00\x00xpw\x07\x03\x00\x00\x07\xE4\x03\x12x' for column 
       'lastDate' at row 1

错误代码:1292

       Call: INSERT INTO flight (FLIGHTID, FLIGHTNAME,LASTDATE, NAME, FIRSTDATE) VALUES (?, ?, ?, ?, ?, ?)
       bind => [5 parameters bound]
       Query: InsertObjectQuery(com.springmvc.jpa.booking.Flight@f0670cc)
       org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
       org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:901)
       javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
       org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:875)
       javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
       org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

根本原因:

       javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence 
       Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
       Internal Exception: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: 
       Incorrect date value: 
       '\xAC\xED\x00\x05sr\x00\x0Djava.time.Ser
        \x95]\x84\xBA\x1B"H\xB2\x0C\x00\x00xpw\x07\x03\x00\x00\x07\xE4\x03\x12x' 
       for column 'lastDate' at row 1

错误代码:1292

       Call: INSERT INTO flight (FLIGHTEID, FLIGHTNAME, LASTDATE, FLIGHTNAME, 
       FIRSTDATE) VALUES (?, ?, 
      ?, ?, ?, ?)
      bind => [5 parameters bound]

这张图片显示了可用的 EclipseLink:

enter image description here

根据您的日志,您使用的是 EclipseLink 2.5,这是 JPA 2.1 的参考实现 (RI) (https://www.eclipse.org/eclipselink/releases/2.5.php)

支持 LocalDate 和其他 Java 8 次 类 添加到 JPA 2.2 中。

您应该更新到 JPA 2.2 的 RI 的 EclipseLink 2.7

我通过添加这个依赖解决了这个问题:

         <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-java8 -->
        <dependency>
           <groupId>org.hibernate</groupId>
             <artifactId>hibernate-java8</artifactId>
             <version>5.4.6.Final</version>
        </dependency>