DataIntegrityViolationException: Error attempting to get column处理方案汇总

x33g5p2x  于2022-05-05 转载在 其他  
字(5.4k)|赞(0)|评价(0)|浏览(393)

项目背景
项目整体采用的是springboot+mybatis 方式。有一次做数据查询的时候。console突然报:DataIntegrityViolationException: Error attempting to get column ‘xx’…异常。起初没在意。以为是xml中的SQL写错了,排查了没问题。百度一下这个报错,说是实体类属性与数据库字段类型不一致引起的,记录一下防止后续在遇到相似问题。

简单写一下出问题的xml及返回值类型:

1 mapper:

<select id="getUserList" paramType="java.lang.Integer" resultType="com.rllc.pcloud.po.UserPo">
    select id,name,date,sign,del from sys_users where id=#{userId}
</select>

2 UserPo:

@Data
public class UserPo{
  private Integer id;
  
  private String name;

  private String password;
  
  // 问题就出在这个有参构造方法中
  public UserPo(String id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    } 
  
}

问题就出在这个有参的构造方法中。而没有无参构造方法。没有无参构造函数时,mybatsi为啥报这个错误呢?此时只有一个包含全部属性的构造函数,mybatis就会找出这些属性对应的值,并实例化一个实体类对象。在找属性对应的值时,mybatis会按照实体类全属性的构造函数的入参顺序,与sql的查询结果对应,如果实体类的属性和sql结果列不能匹配时都会报错。

综上所述,问题定位了,就很好解决:

补上无参构造方法:

public class UserPo{
  private Integer id;
  
  private String name;

  private String password;

  public UserPo(){
  }  

  // 问题就出在这个有参构造方法中
  public UserPo(String id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    } 
}

OK了.

常见的原因汇总
1.封装集合中字段名与数据库列名不一致,检查是否一致以及是否有该字段;
2.使用lombok或是其他操作导致没有无参构造或是无get/set;
3.使用Druid,因为版本问题导致对于时间类型LocalDateTime.class处理异常;此种处理方案:不使用Druid或是使用其他数据库连接源进行替换或是Druid版本升级.
先说一下此异常的出处:
BaseTypeHandler.java中getResult

public T getResult(ResultSet rs, String columnName) throws SQLException {
    try {
      // 结果集中对数据库字段与实体类属性进行映射处理
      return getNullableResult(rs, columnName);
    } catch (Exception e) {
      throw new ResultMapException("Error attempting to get column '" + columnName + "' from result set.  Cause: " + e, e);
    }
  }

此异常之前还会有异常,也就是说该异常会有多种原因导致,也就对应了该异常出现的解决方案会有多种.这里说一下第三种异常处理方案的处理定位处理过程.
问题场景描述
按照日期查询符合条件的课程记录信息,实体类中时间字段:startTime(格式为YYYY-MM-dd HH:mm:ss),数据库中字段start_time,字段类型dateTime.
堆栈异常信息(篇幅原因删减部分堆栈信息):

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'start_time' from result set.  Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'start_time' from result set.  Cause: java.sql.SQLFeatureNotSupportedException
	at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147)
Caused by: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'start_time' from result set.  Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'start_time' from result set.  Cause: java.sql.SQLFeatureNotSupportedException
	at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:87)
	... 73 more
Caused by: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'start_time' from result set.  Cause: java.sql.SQLFeatureNotSupportedException
	at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:85)
	... 99 more
Caused by: java.sql.SQLFeatureNotSupportedException
	at com.alibaba.druid.pool.DruidPooledResultSet.getObject(DruidPooledResultSet.java:1771)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.ibatis.logging.jdbc.ResultSetLogger.invoke(ResultSetLogger.java:69)
	at com.sun.proxy.$Proxy190.getObject(Unknown Source)
	at org.apache.ibatis.type.LocalDateTimeTypeHandler.getNullableResult(LocalDateTimeTypeHandler.java:38)
	at org.apache.ibatis.type.LocalDateTimeTypeHandler.getNullableResult(LocalDateTimeTypeHandler.java:28)
	at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:85)
	... 101 more

看堆栈信息,首先从最开始的位置开始看,很明显是LocalDateTimeTypeHandler.java中getNullableResult抛出的异常,最早出现的异常信息:java.sql.SQLFeatureNotSupportedException.
先看LocalDateTimeTypeHandler.java中getNullableResult

public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
    return rs.getObject(columnName, LocalDateTime.class);
  }

rs.getObject有很多中实现方式,由于使用的是Druid数据库连接源(最初使用的版本是1.0.28),所以直接定位到DruidPooledResultSet.java中getObject:

public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

按照druid1.0.28翻看源码发现对于
关于日期类型处理说明LocalDateTime.class是不支持的,直接抛出SQLFeatureNotSupportedException.修改pom.xml中Druid版本,升级到1.2.1,然后看一下该版本的实现:

public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
        try {
            return this.rs.getObject(columnLabel, type);
        } catch (Throwable var4) {
            throw this.checkException(var4);
        }
    }

说明高版本对LocalDateTime.class处理是支持的,重新测试之后正常!

说明:

org.springframework.dao.DataIntegrityViolationException: Error attempting to get column 'createtime' from result set.  Cause: java.sql.SQLDataException: Unsupported conversion from TIMESTAMP to java.lang.Byte
; Unsupported conversion from TIMESTAMP to java.lang.Byte; nested exception is java.sql.SQLDataException: Unsupported conversion from TIMESTAMP to java.lang.Byte
   at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:84)
   at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)

一个很简单的SQL查询,sql是没有问题。可是 一直报错。。问题出在@Data这注解上。spring

缘由:mybatis在 select * from table 而后映射到实体类的时候,会经过反射实例化,sql

在mybatis-3.5.0.jar 包中org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createByConstructorSignature(ResultSetWrapper, Class<?>, List

解决办法 :一、加上无参构造的注解。,或者本身写个无参构造函数,,mybatis在实例化对象的时候,(会选择第一个构造函数)mybatis

还有问题,就在无参构造函数上加上注解@AutomapConstructor 。。mybatis会选择带这个注解的构造函数

相关文章

微信公众号

最新文章

更多