JPA 中的 getSingleResult() 抛出异常

Posted

技术标签:

【中文标题】JPA 中的 getSingleResult() 抛出异常【英文标题】:getSingleResult() in JPA thorws exceptions 【发布时间】:2013-08-24 00:56:49 【问题描述】:

我正在使用 JPA 进行查询,而当我使用

query.getSingleResult();

它正在抛出异常。我很清楚,如果多个记录与参数匹配,则该方法会引发异常,但在我的情况下,参数是主键,因此这意味着结果将是唯一的。但不是上面的方法,当我使用

query.getResultList();

我没有这样的问题。我很迷惑。同样当我使用该方法时

query.setMaxResults(some value);

然后尝试获取结果列表,我的程序抛出了很多错误..!

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: 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 '2 registered0_.id as id0_, registered0_.current_status as current2_0_, registere' at line 1
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1360)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1288)
    at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:313)
    at com.aces.servlets.UserStatusServlet.getStatus(UserStatusServlet.java:193)
    at com.aces.servlets.UserStatusServlet.access$0(UserStatusServlet.java:188)
    at com.aces.servlets.UserStatusServlet$1.onComplete(UserStatusServlet.java:50)
    at org.apache.catalina.core.AsyncListenerWrapper.fireOnComplete(AsyncListenerWrapper.java:40)
    at org.apache.catalina.core.AsyncContextImpl.fireOnComplete(AsyncContextImpl.java:119)
    at org.apache.coyote.AsyncStateMachine.asyncPostProcess(AsyncStateMachine.java:190)
    at org.apache.coyote.AbstractProcessor.asyncPostProcess(AbstractProcessor.java:116)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:593)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.exception.SQLGrammarException: 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 '2 registered0_.id as id0_, registered0_.current_status as current2_0_, registere' at line 1
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:83)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
    at com.sun.proxy.$Proxy54.executeQuery(Unknown Source)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1962)
    at org.hibernate.loader.Loader.doQuery(Loader.java:829)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
    at org.hibernate.loader.Loader.doList(Loader.java:2447)
    at org.hibernate.loader.Loader.doList(Loader.java:2433)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2263)
    at org.hibernate.loader.Loader.list(Loader.java:2258)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1161)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
    at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:280)
    ... 12 more
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 '2 registered0_.id as id0_, registered0_.current_status as current2_0_, registere' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2293)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
    ... 27 more

【问题讨论】:

你应该提供SSCCE以获得更好的帮助,你甚至不发布你的错误或堆栈跟踪..,你应该提供一些代码以获得更好的帮助。 @nachokk 我所有的堆栈跟踪和错误都发布在我之前的问题中。只需点击我问题末尾的链接即可。 至少包括你收到的异常 @MarkRotteveel 致歉。现在将包括它们:)。 【参考方案1】:

JPA getSingleResult() or null

http://sysout.be/2011/03/09/why-you-should-never-use-getsingleresult-in-jpa/

http://www.objectdb.com/java/jpa/query/execute

【讨论】:

【参考方案2】:

如果查询没有返回结果,getSingleResult() 将抛出 NoResultException。 这种行为使getSingleResult() 不适用于您希望查询返回零个或一个结果的情况。在这种情况下,您需要使用getResultList()

您的setMaxResults() 问题似乎是由于SQL 方言设置不正确

【讨论】:

以上是关于JPA 中的 getSingleResult() 抛出异常的主要内容,如果未能解决你的问题,请参考以下文章

getSingleResult 在休眠中返回本地标量查询的代理

JPA createNamedQuery 语法

getSingleResult() 改变 count() 查询

使用Spring Data JPA查询时,报result returns more than one elements异常

在 getSingleResult 聚合中处理 null 的正确方法

getSingleResult 和 selectone