MySQLSyntaxErrorException: Unknown column ‘crowdcusto0_.batch‘ in ‘field list‘ 解决方案

Posted niaonao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQLSyntaxErrorException: Unknown column ‘crowdcusto0_.batch‘ in ‘field list‘ 解决方案相关的知识,希望对你有一定的参考价值。

异常描述

    版本迭代功能开发后,发布新功能定时任务执行报错。异常原因是 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException,关键异常日志如下

2021-08-25 11:08:00 [cn.nascent.smartcrm.executor.jobhandler.ExecuteNodeTaskHandler]-[getMarketingNodeInstList]-[211]-[pool-6-thread-2] org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
	at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:635)
	at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:106)
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:403)
	at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
	at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:111)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
	at com.sun.proxy.$Proxy81.getFileUrlByCrowdId(Unknown Source)
	at cn.nascent.smartcrm.executor.service.MarketingActivityService.isCustomCrowd(MarketingActivityService.java:1098)
	at cn.nascent.smartcrm.executor.service.MarketingActivityService$$FastClassBySpringCGLIB$$a5a35da4.invoke()
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    ...
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'crowdcusto0_.batch' 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:1054)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2834)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2156)
	at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2313)
	at com.alibaba.druid.pool.DruidPooledPreparedStatement.executeQuery(DruidPooledPreparedStatement.java:227)
	at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
	... 

问题分析

    看到日志包含 com.mysql.jdbc.exceptions 相关异常,我们就知道是数据库交互不规范导致的异常。
    MySQLSyntaxErrorException 是 MySQL 语法错误异常。常见情况比如 SQL 语句错误,语法错误,查询字段不存在,实体类映射不匹配等。

问题处理

    异常后面会指出当前语法错误的具体原因,此处为 Unknown column 'crowdcusto0_.batch' in 'field list',是程序读取到实体类中维护的属性 batch,对应的映射表却没有维护字段 batch。我这是升级功能扩展维护了字段 batch,脚本没刷,数据表结构未修改,导致程序运行时去关联映射找不到,异常了。维护好脚本,重启服务即可。

以上是关于MySQLSyntaxErrorException: Unknown column ‘crowdcusto0_.batch‘ in ‘field list‘ 解决方案的主要内容,如果未能解决你的问题,请参考以下文章

executeUpdate() 抛出 MySQLSyntaxErrorException

MySQLSyntaxErrorException:用户访问被拒绝

错误:MySQLSyntaxErrorException - 拒绝用户选择命令

MySQLSyntaxErrorException:未知数据库 - JDBC - Java EE

如何解决 JDBC PreparedStatement 中的 MySQLSyntaxErrorException [重复]

MySQLSyntaxErrorException