解决SQL Error: 1054, SQLState: 42S22 Unknown column 'markcardex0_.art_service_time' in 'field list'
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: ...
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 ,不对应还是对不上的
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)