我在本机查询中没有选择的列出现异常
Posted
技术标签:
【中文标题】我在本机查询中没有选择的列出现异常【英文标题】:Got exception about columns I didn't select in native query 【发布时间】:2015-09-21 19:43:30 【问题描述】:我的环境是 Spring 4.0.5、hibernate 4.3.6、hsql 2.3.2。在生产中,它将连接到 mysql。 执行本机查询时,我不断收到未找到的列。
这是我的实体
@Entity
@Table(name = "taxonomy")
public class Taxonomy implements AwrDO, Searchable
@Id
@Column(name = "taxonomy_sid", nullable = false)
private Long sid;
@Column(name = "taxonomy_id", nullable = false)
private Long taxonomyId;
@Column(name = "region_id", nullable = false)
private Integer regionId;
@Column(name = "taxonomy_name")
private String taxonomyName;
// weird column
@Column(name = "event_type_id")
private Integer evnetTypeId;
我命名的本机查询和 SqlResultSetMapping 是
@NamedNativeQuery(
name = "getNextLevelFromIdAndRegion",
query = "select t.taxonomy_sid, t.taxonomy_id, t.region_id, t.taxonomy_name " +
"from ... ",
resultSetMapping = "getNextLevelFromIdAndRegion"
)
@SqlResultSetMapping(name = "getNextLevelFromIdAndRegion", entities = @EntityResult(entityClass = Taxonomy.class, fields =
@FieldResult(name = "sid", column = "taxonomy_sid"),
@FieldResult(name = "taxonomyId", column = "taxonomy_id"),
@FieldResult(name = "regionId", column = "region_id"),
@FieldResult(name = "taxonomyName", column = "taxonomy_name")
)
所以在我的本机查询中,我只对前四列感兴趣,我只选择那些并只在结果集映射中指定那些。
但如果我这样做了,我会收到异常抱怨Caused by: java.sql.SQLException: Column 'event_ty4_8_0_' not found.
,甚至认为本机查询在 mysql 中有效。
一种解决方法是选择@Entity 中定义的所有列,并将它们全部放入SqlResultSetMapping。我如何定义我的实体有什么问题吗?
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:458) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at awr.dao.TaxonomyDAO.getNextLevelFromIdAndRegion(TaxonomyDAO.java:21) ~[aiy-awr.jar:na]
at awr.dao.TaxonomyDAO$$FastClassBySpringCGLIB$$56e60e38.invoke(<generated>) ~[spring-core-4.0.5.RELEASE.jar:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708) ~[spring-aop-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) ~[spring-tx-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) ~[spring-tx-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) ~[spring-tx-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644) ~[spring-aop-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at awr.dao.TaxonomyDAO$$EnhancerBySpringCGLIB$$3e0055aa.getNextLevelFromIdAndRegion(<generated>) ~[spring-core-4.0.5.RELEASE.jar:na]
at awr.services.report.TaxonomyTreeService.runReport(TaxonomyTreeService.java:38) ~[aiy-awr.jar:na]
at awr.services.report.AwrDataService.runReport(AwrDataService.java:76) ~[aiy-awr.jar:na]
at awr.services.report.AwrReportService.run(AwrReportService.java:46) ~[aiy-awr.jar:na]
at aiy.mw.ws.ReportResource.run(ReportResource.java:71) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_40]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_40]
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:167) [resteasy-jaxrs-2.3.6.Final.jar:na]
at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:269) [resteasy-jaxrs-2.3.6.Final.jar:na]
at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:227) [resteasy-jaxrs-2.3.6.Final.jar:na]
at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:216) [resteasy-jaxrs-2.3.6.Final.jar:na]
at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:542) [resteasy-jaxrs-2.3.6.Final.jar:na]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:524) [resteasy-jaxrs-2.3.6.Final.jar:na]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:126) [resteasy-jaxrs-2.3.6.Final.jar:na]
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.6.Final.jar:na]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.6.Final.jar:na]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.6.Final.jar:na]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [servlet-api-3.1.jar:3.1.0]
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:808) [jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1669) [jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
at aiy.mw.web.filter.SessionTimeoutFilter.doFilter(SessionTimeoutFilter.java:38) [classes/:na]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
at aiy.mw.web.filter.WssidFilter.doFilter(WssidFilter.java:97) [classes/:na]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
at aiy.mw.web.filter.CorsResponseFilter.doFilter(CorsResponseFilter.java:25) [classes/:na]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.servlets.UserAgentFilter.doFilter(UserAgentFilter.java:83) [jetty-servlets-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.servlets.GzipFilter.doFilter(GzipFilter.java:364) [jetty-servlets-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
at yjava.servlet.filter.SSLCrimeFilter.doFilter(SSLCrimeFilter.java:30) [yjava_servlet_filters.jar:na]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
at yjava.servlet.filter.YHdrsFilter.doFilter(YHdrsFilter.java:73) [yjava_servlet_filters.jar:na]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
at yjava.cookie.CookieDataFilter.doFilter(CookieDataFilter.java:133) [yjava_cookie_data_servlet_filter.jar:na]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
at yjava.servlet.filter.DoNotTrackFilter.doFilter(DoNotTrackFilter.java:114) [yjava_servlet_filters.jar:na]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
at yjava.remote.ip.RemoteIPFilter.doFilter(RemoteIPFilter.java:126) [yjava_remote_ip_servlet_filter.jar:na]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
at yjava.security.yiv.servlet.InputValidationFilter.doFilter(InputValidationFilter.java:228) [yjava_yiv_servlet.jar:na]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
at yjava.servlet.filter.StatsFilter.doFilter(StatsFilter.java:73) [yjava_servlet_filters.jar:na]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.servlets.UserAgentFilter.doFilter(UserAgentFilter.java:83) [jetty-servlets-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.servlets.GzipFilter.doFilter(GzipFilter.java:364) [jetty-servlets-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585) [jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577) [jetty-security-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) [jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:215) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:110) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.webapp.logging.ContextLogHandler.handle(ContextLogHandler.java:62) [jetty-webapp-logging.jar:na]
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.server.handler.StatisticsHandler.handle(StatisticsHandler.java:159) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.server.Server.handle(Server.java:497) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) [jetty-io-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) [jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) [jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_40]
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2556) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2539) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.loader.Loader.list(Loader.java:2364) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:353) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1873) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:311) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:141) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
... 81 common frames omitted
Caused by: java.sql.SQLException: Column 'event_ty4_8_0_' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074) ~[mysql_connector_java.jar:na]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988) ~[mysql_connector_java.jar:na]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974) ~[mysql_connector_java.jar:na]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919) ~[mysql_connector_java.jar:na]
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1167) ~[mysql_connector_java.jar:na]
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2851) ~[mysql_connector_java.jar:na]
at org.apache.commons.dbcp2.DelegatingResultSet.getInt(DelegatingResultSet.java:283) ~[commons-dbcp2-2.0.1.jar:2.0.1]
at org.apache.commons.dbcp2.DelegatingResultSet.getInt(DelegatingResultSet.java:283) ~[commons-dbcp2-2.0.1.jar:2.0.1]
at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$2.doExtract(IntegerTypeDescriptor.java:74) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:267) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:263) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:338) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2969) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1695) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1627) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.loader.Loader.getRow(Loader.java:1514) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:725) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.loader.Loader.processResultSet(Loader.java:952) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.loader.Loader.doQuery(Loader.java:920) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2553) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
... 90 common frames omitted
【问题讨论】:
@TobiasLiefke:其实很简单的from _TABLE_
就足以触发这个问题
【参考方案1】:
来自@EntityResult
的Javadoc:
用于将 SQL 查询的 SELECT 子句映射到实体结果。 如果使用此注解,则 SQL 语句应选择 所有 列映射到实体对象。 这应该包括相关实体的外键列。 数据不足时得到的结果 未定义。
【讨论】:
好的,所以我的解决方法实际上是编写它的必需方法,谢谢。以上是关于我在本机查询中没有选择的列出现异常的主要内容,如果未能解决你的问题,请参考以下文章
核心数据异常:向实体添加新属性后出现 SQLite“没有这样的列”错误