我的環境是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:其實一個死的簡單'從_TABLE_'足以引發這一問題, –