Hibernate version: hibernate-3.2.2.GA, hibernate-entitymanager-3.3.0.GA, hibernate-annotations-3.3.0.GA
Hi all,
I'm using hibernate with entity manager and annotations. I have the following code (please note the positional parameter used):
Code:
Query query = getEntityManager().createQuery("SELECT f FROM Application a JOIN a.fields f WHERE a.id = ?1");
query.setParameter(1, new Long(1));
if (_logger.isInfoEnabled()) {
_logger.info("About to execute query [ " + query + "]");
}
return query.getResultList();
The query is using one positional parameter
Code:
a.id = ?1
, following the syntax dictated by JSR 220 where positional parameters are formed by the '?' followed by an integer. When I execute it, I get the following exception:
Quote:
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
And looking at the stack trace, I found this:
Quote:
Caused by: java.sql.SQLException: Unexpected token: 1 in statement [..]
If I remove the integer following the question mark (and hence, use positional parameters like plain jdbc prepared statement parameters), everything works OK. Am I missing something, because after reading hibernate docs and examples for hibernate entity manager, I see they use the JSR 220 syntax for positional parameters, and it is said to be supported.
As I see it, EJBQL query parameters are being parsed as normal HQL or JDBC prepared statement parameters.
Posting part of the stack trace here:
Code:
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
[junit] at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source)
[junit] at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
[junit] at org.apache.commons.dbcp.DelegatingConnection.prepareStatement(DelegatingConnection.java:248)
[junit] at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.prepareStatement(PoolingDataSource.java:302)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[junit] at java.lang.reflect.Method.invoke(Method.java:597)
[junit] at org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy$LazyConnectionInvocationHandler.invoke(LazyConnectionDataSourceProxy.java:338)
[junit] at $Proxy23.prepareStatement(Unknown Source)
[junit] at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:497)
[junit] at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:415)
[junit] at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:139)
[junit] at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1538)
[junit] at org.hibernate.loader.Loader.doQuery(Loader.java:661)
[junit] at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
[junit] at org.hibernate.loader.Loader.doList(Loader.java:2211)
[junit] at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
[junit] at org.hibernate.loader.Loader.list(Loader.java:2090)
[junit] at org.hibernate.hql.classic.QueryTranslatorImpl.list(QueryTranslatorImpl.java:912)
[junit] at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
[junit] at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
[junit] at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
[junit] at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:66)
Any help will be deeply appreciated, cheers!!!!