调用存储过程(MySQL)会从Hibernate中抛出异常

时间:2016-05-18 06:16:11

标签: java mysql hibernate stored-procedures

我通过hibernate调用了mySQL存储过程,但它总是给我错误。

以下是代码 -

SQLQuery query = session.createSQLQuery("call test()").addEntity(Organisation.class);
List result = query.list();

以下是错误堆栈跟踪 -

    org.hibernate.exception.SQLGrammarException: could not extract ResultSet
[INFO]  at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82)
[INFO]  at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
[INFO]  at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
[INFO]  at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
[INFO]  at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:61)
[INFO]  at org.hibernate.loader.Loader.getResultSet(Loader.java:2031)
[INFO]  at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1832)
[INFO]  at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1811)
[INFO]  at org.hibernate.loader.Loader.doQuery(Loader.java:899)
[INFO]  at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
[INFO]  at org.hibernate.loader.Loader.doList(Loader.java:2516)
[INFO]  at org.hibernate.loader.Loader.doList(Loader.java:2502)
[INFO]  at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2332)
[INFO]  at org.hibernate.loader.Loader.list(Loader.java:2327)
[INFO]  at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:338)
[INFO]  at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1826)
[INFO]  at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:231)
[INFO]  at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:157)
[INFO]  at com.ma.rocket.v1.modules.app.dao.SalesReportDao.getSalesInfoByCountry(SalesReportDao.java:39)
[INFO]  at com.ma.rocket.v1.modules.app.service.SalesReportService.getSalesInfoByCountry(SalesReportService.java:14)
[INFO]  at com.ma.rocket.v1.modules.app.endpoints.UserReportGenerationEP.getSalesReportByCountry(UserReportGenerationEP.java:18)
[INFO]  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[INFO]  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[INFO]  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[INFO]  at java.lang.reflect.Method.invoke(Method.java:606)
[INFO]  at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:130)
[INFO]  at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:359)
[INFO]  at com.google.api.server.spi.SystemServiceServlet.execute(SystemServiceServlet.java:113)
[INFO]  at com.google.api.server.spi.SystemServiceServlet.doPost(SystemServiceServlet.java:71)
[INFO]  at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
[INFO]  at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
[INFO]  at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
[INFO]  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
[INFO]  at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
[INFO]  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[INFO]  at com.google.appengine.tools.development.DevAppServerModulesFilter.doRedirectedModuleRequest(DevAppServerModulesFilter.java:415)
[INFO]  at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:128)
[INFO]  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[INFO]  at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
[INFO]  at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
[INFO]  at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
[INFO]  at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
[INFO]  at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
[INFO]  at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:98)
[INFO]  at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:327)
[INFO]  at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)
[INFO]  at com.google.appengine.tools.development.JettyContainerService.forwardToServer(JettyContainerService.java:449)
[INFO]  at com.google.appengine.tools.development.Modules.forwardToInstance(Modules.java:371)
[INFO]  at com.google.appengine.tools.development.DelegatingModulesFilterHelper.forwardToInstance(DelegatingModulesFilterHelper.java:95)
[INFO]  at com.google.appengine.tools.development.DevAppServerModulesFilter.doRedirect(DevAppServerModulesFilter.java:326)
[INFO]  at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:119)
[INFO]  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[INFO]  at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
[INFO]  at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
[INFO]  at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
[INFO]  at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
[INFO]  at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
[INFO]  at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:98)
[INFO]  at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
[INFO]  at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:502)
[INFO]  at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
[INFO]  at org.mortbay.jetty.Server.handle(Server.java:326)
[INFO]  at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
[INFO]  at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
[INFO]  at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
[INFO]  at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
[INFO]  at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
[INFO]  at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
[INFO]  at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
[INFO] Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1
[INFO]  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[INFO]  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
[INFO]  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[INFO]  at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
[INFO]  at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance_(Runtime.java:142)
[INFO]  at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance(Runtime.java:150)
[INFO]  at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
[INFO]  at com.mysql.jdbc.Util.getInstance(Util.java:384)
[INFO]  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
[INFO]  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232)
[INFO]  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164)
[INFO]  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:926)
[INFO]  at com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:2444)
[INFO]  at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2834)
[INFO]  at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2838)
[INFO]  at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
[INFO]  at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2212)
[INFO]  at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:56)
[INFO]  ... 64 

以下是存储过程的代码,它完美地运行 -

DELIMITER ;;
CREATE PROCEDURE `test`()
BEGIN

SELECT * FROM `Dim_Organisation`;
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

END;;
DELIMITER ;

1 个答案:

答案 0 :(得分:0)

我通过从存储过程中删除以下语句来解决这个问题

 PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;