当前位置: 代码迷 >> 综合 >> 解决SQL Error: 1054, SQLState: 42S22 Unknown column 'markcardex0_.art_service_time' in 'field list'
  详细解决方案

解决SQL Error: 1054, SQLState: 42S22 Unknown column 'markcardex0_.art_service_time' in 'field list'

热度:39   发布时间:2023-11-10 11:15:49.0

SQL Error: 1054, SQLState: 42S22 这是字段缺失导致的错误,先附上错误日志:

2020-03-04 10:30:00.221 WARN  [pool-8-thread-1] [org.hibernate.engine.jdbc.spi.SqlExceptionHelper.java:127] - SQL Error: 1054, SQLState: 42S22
2020-03-04 10:30:00.221 ERROR [pool-8-thread-1] [org.hibernate.engine.jdbc.spi.SqlExceptionHelper.java:129] - Unknown column 'markcardex0_.art_service_time' in 'field list'
2020-03-04 10:30:00.225 ERROR [pool-8-thread-1] [org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler.java:95] - Unexpected error occurred in scheduled task.
org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.……

……


Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    ... 24 common frames omitted
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'markcardex0_.art_service_time' in 'field list'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2719)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2318)
    at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeQuery(FilterChainImpl.java:2714)
    at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_executeQuery(FilterEventAdapter.java:465)
    at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeQuery(FilterChainImpl.java:2711)
    at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.executeQuery(PreparedStatementProxyImpl.java:145)
    at com.alibaba.druid.pool.DruidPooledPreparedStatement.executeQuery(DruidPooledPreparedStatement.java:227)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
    ... 60 common frames omitted

 

出现这个错误,主要是实体类中的变量和数据库表中的类对不上,这是一个让人比较头疼的问题,特别是数据复杂的时候。以前比较笨,一个个对,现在总结一个比较简单的方法:

1、看这个错误原因:Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'markcardex0_.art_service_time' in 'field list' ,就是因为art_service_time这个属性和实体类中的变量对不上,使用快捷键 Ctrl+Shift+F (也可以打开 Edit --> Find -->  Find In Path )快速查找 artServiceTime,找到定义变量artServiceTime的实体类,点击进去,如下:

 2、进去该变量对应的实体类中之后,通过注解@Table(name = "uk_markcard_xxxx")可以看见该实体对应的数据库表:

       打开数据库,通过查询验证该表是不是存在该属性,如下:

通过上图, 发现了和控制台一样的错误,这个时候我们就在这个表上添加对应实体变量的属性了,注意:

    (1)属性类型要和该实体变量类型一致哦

    (2)MySQL中没有大小写的区别,但是如果实体类中变量名为 aaaBcc 的时候,数据库中属性名应为 aaa_bcc ,不对应还是对不上的

  相关解决方案