使用hibernate报错:
[2022-04-18 14:18:03] [WARN] JDBCExceptionReporter: SQL Error: 1054, SQLState: 42S22 [2022-04-18 14:18:03] [ERROR] JDBCExceptionReporter: Unknown column 'account0_.note' in 'field list' [2022-04-18 14:18:06] [ERROR] EcMeituanReportCrawlerModule: could not execute query org.hibernate.exception.SQLGrammarException: could not execute query at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate.loader.Loader.doList(Loader.java:2536) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276) at org.hibernate.loader.Loader.list(Loader.java:2271) at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:452) at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363) at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1268) at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102) at com.xxxxx.framework.dynamicdao.processor.QueryMethodProcessor.executeQuery(QueryMethodProcessor.java:178) at com.xxxxx.framework.dynamicdao.processor.QueryMethodProcessor.process(QueryMethodProcessor.java:79) at com.xxxxx.framework.dynamicdao.DynamicDaoInvocationHandler.invoke(DynamicDaoInvocationHandler.java:78) at com.sun.proxy.$Proxy39.getListAccountByType(Unknown Source) at com.xxxxx.service.weibo.AccountServiceImpl.getListAccountByType(AccountServiceImpl.java:58) at com.xxxxx.service.weibo.AccountServiceImpl$$FastClassByCGLIB$$30b183b2.invoke(原因) at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622) at com.xxxxx.service.weibo.AccountServiceImpl$$EnhancerByCGLIB$$dea806b9.getListAccountByType( ) at com.xxxxx.ec.crawler.module.meituan.report.EcMeituanReportCrawlerModule.run(EcMeituanReportCrawlerModule.java:62) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.runAndReset$$$capture(FutureTask.java:308) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'account0_.note' 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:425) at com.mysql.jdbc.Util.getInstance(Util.java:408) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2487) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1966) at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96) at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96) at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208) at org.hibernate.loader.Loader.getResultSet(Loader.java:1953) at org.hibernate.loader.Loader.doQuery(Loader.java:802) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274) at org.hibernate.loader.Loader.doList(Loader.java:2533) ... 29 more
核心提示是 Unknown column 'account0_.note' in 'field list',即note 字段有问题。
首先,我们先检查实体类,在实体类中有 note 这个字段。
其次,我们检查实体类对应的数据库表中,是否有 note 字段。
所以问题出在实体类中有 note 字段,而数据库表中没有对应的字段,所以映射失败。
解决要么在数据库表中添加该字段,要么删除掉实体类中该字段,要么再创建一个实体类来映射这张没有该字段的表。
之所以要贴出另外一张有该字段的图,是因为生产环境和测试环境的表字段可能不一样,需要注意咨询检查。
注,我采用的是另外创建一个没有 note 字段的同名实体类的方式来解决这个问题,但并没有完全解决。因为它还是使用的是之前那个有 note 字段的实体类。后来我发现 dao 层的查询操作是这样写的:
@Query("from Account where type = ?")
List getAccountListByType(Long type);
对,这里的 Account 映射的是实体类类名。我想这个 Account 是否是之前那个有 note 字段的实体类,而非我自己创建的没有 note 字段的实体类。不太熟悉hibernate,所以不清楚具体如何判断这个 Account 到底是哪一个类。所以我将 @Query 注解内的内容改为如下重新运行:
@Query(sqlQuery = true, entityClass = Account.class, value = "select * from account where type = ?") ListgetAccountListByType(Long type);
成功解决了这个问题,因为 entityClass 属性可以明确指定你使用的是哪个 Account 类。注意,不要是如下,不会成功:
@Query("select * from account where type = ?")
List getAccountListByType(Long type);
注意:
- 应该避免同名类的出现。我这是迫不得己,生产环境的类不能随便修改,只能自己新建。
- 如果是 maven 项目,可以清理掉之前的代码和本地仓库的包,然后重新将修改后的实体类打包安装到本地仓库中,减少影响。
- hibernate使用中如果遇到同名类尽量不要使用通过类名映射的方式,容易产生误会。



