Hibernate参数索引超出范围,但我没有使用编号参数

时间:2012-01-05 13:37:05

标签: java hibernate jpa jpql

此查询:

em.createQuery("SELECT p FROM Player p WHERE p.displayName LIKE :q ESCAPE '\\'", Player.class)
.setParameter("q", "%" + query.replace("_", "\\_") + "%")
.setMaxResults(20)
.getResultList()

结果

java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).

为什么呢?我没有在查询中使用编号参数,只是名为one(q)的参数,然后我可以看到正确绑定。

完整性是完整的堆栈跟踪。

javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute query
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1215) ~[hibernate-entitymanager-jar-hibernate-entitymanager-3.6.6.Final.jar:3.6.6.Final]
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1148) ~[hibernate-entitymanager-jar-hibernate-entitymanager-3.6.6.Final.jar:3.6.6.Final]
        at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:255) ~[hibernate-entitymanager-jar-hibernate-entitymanager-3.6.6.Final.jar:3.6.6.Final]
        at tr.example.data.DataServer$RemoteDataServerImpl.searchPlayer(DataServer.java:922) ~[dataserver.jar:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0]
        at java.lang.reflect.Method.invoke(Method.java:601) ~[na:1.7.0]
        at tr.example.data.DataServer$1.invoke(DataServer.java:169) ~[dataserver.jar:na]
        at $Proxy4.searchPlayer(Unknown Source) [na:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0]
        at java.lang.reflect.Method.invoke(Method.java:601) ~[na:1.7.0]
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322) [na:1.7.0]
        at sun.rmi.transport.Transport$1.run(Transport.java:177) [na:1.7.0]
            at sun.rmi.transport.Transport$1.run(Transport.java:174) [na:1.7.0]
        at java.security.AccessController.doPrivileged(Native Method) [na:1.7.0]
        at sun.rmi.transport.Transport.serviceCall(Transport.java:173) [na:1.7.0]
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553) [na:1.7.0]
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808) [na:1.7.0]
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667) [na:1.7.0]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [na:1.7.0]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [na:1.7.0]
        at java.lang.Thread.run(Thread.java:722) [na:1.7.0]
Caused by: org.hibernate.exception.GenericJDBCException: could not execute query
        at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140) ~[hibernate-core-jar-hibernate-core-3.6.7.Final.jar:3.6.7.Final]
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128) ~[hibernate-core-jar-hibernate-core-3.6.7.Final.jar:3.6.7.Final]
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) ~[hibernate-core-jar-hibernate-core-3.6.7.Final.jar:3.6.7.Final]
        at org.hibernate.loader.Loader.doList(Loader.java:2536) ~[hibernate-core-jar-hibernate-core-3.6.7.Final.jar:3.6.7.Final]
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276) ~[hibernate-core-jar-hibernate-core-3.6.7.Final.jar:3.6.7.Final]
        at org.hibernate.loader.Loader.list(Loader.java:2271) ~[hibernate-core-jar-hibernate-core-3.6.7.Final.jar:3.6.7.Final]
        at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:452) ~[hibernate-core-jar-hibernate-core-3.6.7.Final.jar:3.6.7.Final]
        at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363) ~[hibernate-core-jar-hibernate-core-3.6.7.Final.jar:3.6.7.Final]
        at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196) ~[hibernate-core-jar-hibernate-core-3.6.7.Final.jar:3.6.7.Final]
        at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1268) ~[hibernate-core-jar-hibernate-core-3.6.7.Final.jar:3.6.7.Final]
        at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102) ~[hibernate-core-jar-hibernate-core-3.6.7.Final.jar:3.6.7.Final]
        at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:246) ~[hibernate-entitymanager-jar-hibernate-entitymanager-3.6.6.Final.jar:3.6.6.Final]
        ... 22 common frames omitted
Caused by: java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) ~[mysql-connector-java-jar-mysql-connector-java-5.1.14.jar:na]
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) ~[mysql-connector-java-jar-mysql-connector-java-5.1.14.jar:na]
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) ~[mysql-connector-java-jar-mysql-connector-java-5.1.14.jar:na]
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927) ~[mysql-connector-java-jar-mysql-connector-java-5.1.14.jar:na]
        at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3709) ~[mysql-connector-java-jar-mysql-connector-java-5.1.14.jar:na]
        at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3693) ~[mysql-connector-java-jar-mysql-connector-java-5.1.14.jar:na]
        at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3735) ~[mysql-connector-java-jar-mysql-connector-java-5.1.14.jar:na]
        at com.mysql.jdbc.PreparedStatement.setInt(PreparedStatement.java:3679) ~[mysql-connector-java-jar-mysql-connector-java-5.1.14.jar:na]
        at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.setInt(NewProxyPreparedStatement.java:677) ~[c3p0-jar-c3p0-0.9.1.jar:0.9.1]
        at org.hibernate.loader.Loader.bindLimitParameters(Loader.java:1812) ~[hibernate-core-jar-hibernate-core-3.6.7.Final.jar:3.6.7.Final]
        at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1719) ~[hibernate-core-jar-hibernate-core-3.6.7.Final.jar:3.6.7.Final]
        at org.hibernate.loader.Loader.doQuery(Loader.java:801) ~[hibernate-core-jar-hibernate-core-3.6.7.Final.jar:3.6.7.Final]
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274) ~[hibernate-core-jar-hibernate-core-3.6.7.Final.jar:3.6.7.Final]
        at org.hibernate.loader.Loader.doList(Loader.java:2533) ~[hibernate-core-jar-hibernate-core-3.6.7.Final.jar:3.6.7.Final]
        ... 30 common frames omitted

1 个答案:

答案 0 :(得分:2)

通过双重逃避反斜杠修正了它:

em.createQuery("SELECT p FROM Player p WHERE p.displayName LIKE :q ESCAPE '\\\\'", Player.class)
.setParameter("q", "%" + query.replace("_", "\\\\_") + "%")
.setMaxResults(20)
.getResultList()

但我认为错误信息并不合适。