DataIntegrityViolationException, DataConversionException abnormal solution
1. Problem phenomenon
When using MyBatis to query data, an exception occurs in the data mapping link:
org.springframework.dao.DataIntegrityViolationException
(Appearance exception) Data integrity constraint exception, usually caused by data type mismatch when data is updated/inserted
com.mysql.cj.exceptions.DataConversionException
(Root Exception) An exception occurred during data conversion
Main error message:
org.springframework.dao.DataIntegrityViolationException: Error attempting to get column 'user_id' from result set. Cause: java.sql.SQLDataException: Unsupported conversion from LONG to java.time.LocalDateTime; Unsupported conversion from LONG to java.time.LocalDateTime; nested exception is java.sql.SQLDataException: Unsupported conversion from LONG to java.time.LocalDateTime] with root cause com.mysql.cj.exceptions.DataConversionException: Unsupported conversion from LONG to java.time.LocalDateTime
Two, the cause of the problem
The model mapped by the query result uses lombok's @
Builder annotation. The class that uses this annotation will only generate a constructor with full parameters for the class at compile time , and will not generate a constructor with no parameters . Then the Model must be instantiated When assigning values through the constructor, if the returned columns are inconsistent with the order of the constructor parameters, this problem will result
1. Example of problem code: Model
Source File
@Builder @Data public class Order { private long id; private long user_id; private LocalDateTime submit_time; private int status; /*Omit other fields*/ }
compile result
You can view the Order.class file in the target directory of the compilation result
public class Order { private long id; private long user_id; private LocalDateTime submit_time; private int status; Order(final long id, final long user_id, final LocalDateTime submit_time, final int status) { this.id = id; this. user_id = user_id; this.submit_time = submit_time; this.status = status; } /*Omit the get and set methods*/ }
2. Problem code example: Mapper
@Mapper public interface OrderMapper { @Select("SELECT id, user_id, status, submit_time FROM order") List<Order> getAllOrder(); }
Three, the solution
You can solve the problem by adding a no-argument constructor or adjusting the order of the query columns. You can choose one of the two.
1. Annotate to add a parameterless constructor (recommended)
Use @
NoArgsConstructor, @
AllArgsConstructor, so that lombok Model has both full-parameter and parameterless constructors
@Builder @Data @NoArgsConstructor @AllArgsConstructor public class Order { private long id; private long user_id; private LocalDateTime submit_time; private int status; /*Omit other fields*/ }
2. Manually add a parameterless constructor
Manually add the constructor and use the @
Tolerate annotation to let lombok ignore the constructor
@Builder @Data public class Order { private long id; private long user_id; private LocalDateTime submit_time; private int status; /*Omit other fields*/ @Tolerate Order() {} }
3. Adjust the order of query columns
@Mapper public interface OrderMapper { @Select("SELECT id, user_id, submit_time, status FROM order") List<Order> getAllOrder(); }
0 Comments