Search code examples
mysqljpaeclipselinklocaldate

LocalDate causes Data truncation: Incorrect date value


I am trying to build an application that handles user's inputs with different data types using mysql and jpa. When I run the application I get an error of "MysqlDataTruncation: Data truncation: Incorrect date value:". I really need help in solving this problem and I already Made some reseach but I was not able to figure it out. I am using Date type in mysql and LocalDate type for my entity class. Is it possible?

This is my first entity 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;

Booking entity class:

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

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

Database 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,

Booking table:

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

)

Error message: Exception

        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

Error Code: 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)

Root Cause:

       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

Error Code: 1292

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

This is an image shows the EclipseLink that are available:

enter image description here


Solution

  • According to your logs you are using EclipseLink 2.5, that is the reference implementation (RI) of JPA 2.1 (https://www.eclipse.org/eclipselink/releases/2.5.php)

    Suport for LocalDate and the other Java 8 time classes where added in JPA 2.2.

    You should update to EclipseLink 2.7 that is the RI of JPA 2.2