java org.hibernate.exception.GenericJDBCException:无法执行查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38921609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:53:31  来源:igfitidea点击:

org.hibernate.exception.GenericJDBCException: could not execute query

javamysqlhibernate

提问by Jordan Aspinall

Upon trying to convert strings to dates for comparison, Hibernate throws

在尝试将字符串转换为日期进行比较时,Hibernate 抛出

org.hibernate.exception.GenericJDBCException: could not execute query

org.hibernate.exception.GenericJDBCException:无法执行查询

full stacktrace:

完整的堆栈跟踪:

    javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute query
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1315)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:255)
    at com.callsystem.business.callFinder.findCustom(callFinder.java:215)
    at com.callsystem.UI.searchCustomerCallsView.buttonClick(searchCustomerCallsView.java:99)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
    at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:1008)
    at com.vaadin.ui.Button.fireClick(Button.java:377)
    at com.vaadin.ui.Button.click(Button.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:158)
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118)
    at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:408)
    at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:273)
    at com.vaadin.server.communication.PushHandler.run(PushHandler.java:149)
    at com.vaadin.server.communication.PushHandler.callWithUi(PushHandler.java:243)
    at com.vaadin.server.communication.PushHandler.onMessage(PushHandler.java:503)
    at com.vaadin.server.communication.PushAtmosphereHandler.onMessage(PushAtmosphereHandler.java:88)
    at com.vaadin.server.communication.PushAtmosphereHandler.onRequest(PushAtmosphereHandler.java:78)
    at org.atmosphere.cpr.AsynchronousProcessor.action(AsynchronousProcessor.java:199)
    at org.atmosphere.cpr.AsynchronousProcessor.suspended(AsynchronousProcessor.java:107)
    at org.atmosphere.container.TomcatWebSocketUtil.doService(TomcatWebSocketUtil.java:154)
    at org.atmosphere.container.Tomcat7Servlet30SupportWithWebSocket.service(Tomcat7Servlet30SupportWithWebSocket.java:62)
    at org.atmosphere.cpr.AtmosphereFramework.doCometSupport(AtmosphereFramework.java:2075)
    at org.atmosphere.websocket.DefaultWebSocketProcessor.dispatch(DefaultWebSocketProcessor.java:571)
    at org.atmosphere.websocket.DefaultWebSocketProcessor.run(DefaultWebSocketProcessor.java:333)
    at org.atmosphere.util.VoidExecutorService.execute(VoidExecutorService.java:101)
    at org.atmosphere.websocket.DefaultWebSocketProcessor.dispatch(DefaultWebSocketProcessor.java:328)
    at org.atmosphere.websocket.DefaultWebSocketProcessor.invokeWebSocketProtocol(DefaultWebSocketProcessor.java:425)
    at org.atmosphere.websocket.DefaultWebSocketProcessor.dispatchReader(DefaultWebSocketProcessor.java:795)
    at org.atmosphere.websocket.DefaultWebSocketProcessor.invokeWebSocketProtocol(DefaultWebSocketProcessor.java:527)
    at org.atmosphere.container.TomcatWebSocketHandler.onTextData(TomcatWebSocketHandler.java:104)
    at org.apache.catalina.websocket.StreamInbound.doOnTextData(StreamInbound.java:190)
    at org.apache.catalina.websocket.StreamInbound.onData(StreamInbound.java:138)
    at org.apache.coyote.http11.upgrade.UpgradeProcessor.upgradeDispatch(UpgradeProcessor.java:88)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:599)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.hibernate.exception.GenericJDBCException: could not execute query
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.doList(Loader.java:2545)
    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:459)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:365)
    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 org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:246)
    ... 46 more
    Caused by: java.sql.SQLException: No database selected
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1086)
    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 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:2542)
    ... 54 more

HQl statement used:

使用的 HQl 语句:

 String hql = "FROM CallSystemCustomerCallEntity WHERE str(refno) LIKE :refNo AND "
                    + "customer LIKE :customer AND site LIKE :site AND faults LIKE :faults "
                    + "AND detailedfaults LIKE :dfaults AND owner LIKE :owner "
                    + "AND date(to_date(date, 'dd-MM-yy')) >= :sDate AND "
                    + "date(to_date(date, 'dd-MM-yy')) <= :eDate ORDER BY refno asc";

Creating the query:

创建查询:

List<CallSystemCustomerCallEntity> c = em.createQuery(hql).
                    setParameter("refNo", "%" + refNo + "%").setParameter("customer", "%" + customer + "%")
                    .setParameter("site", "%" + site + "%").setParameter("faults", "%" + faults + "%")
                    .setParameter("dfaults", "%" + dfaults + "%").setParameter("owner", "%" + owner + "%")
                    .setParameter("sDate",date1).setParameter("eDate", date2).getResultList();

The database in question is a MySQL database and all other queries I try work, however inclusion of the date paremeters cause this exception to be thrown.

有问题的数据库是 MySQL 数据库,我尝试使用的所有其他查询都可以工作,但是包含日期参数会导致抛出此异常。

采纳答案by PCO

Just guessing : you call to_date function but as it is not a hql function it is sent as-is to the database. Mysql receive a sql request with a "to_date" function, but mysql don't have to_date function (it's Oracle specific, you have to use STR_TO_DATE). So mysql search the function as a custom function in the current database but complain that no database have been selected...

只是猜测:您调用 to_date 函数,但由于它不是 hql 函数,因此按原样发送到数据库。Mysql 收到一个带有“to_date”函数的 sql 请求,但 mysql 没有 to_date 函数(这是 Oracle 特定的,你必须使用 STR_TO_DATE)。所以mysql在当前数据库中搜索该函数作为自定义函数,但抱怨没有选择任何数据库......

So, remove to_date (and the date()) and try something with STR_TO_DATE (see https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html).

因此,删除 to_date(和 date())并尝试使用 STR_TO_DATE(参见https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html)。

Edit: and use debug log from hibernate to see the sql sent to the database => the stacktrace show that the exception come from the database, with the sql you can test the query against the database : it's helpful for debugging

编辑:并使用 hibernate 中的调试日志查看发送到数据库的 sql => 堆栈跟踪显示异常来自数据库,使用 sql 您可以针对数据库测试查询:它有助于调试