Moving Hibernate Application to JBoss - Position Out of Number of Declared Orders
I have a Hibernate web application currently working on tomcat / CloudFoundry, but when I try to run it on JBoss I get some errors.
In my DAO, I create a request to load users by username like this:
Query query = getEntityManager().createQuery("select u from com.tmm.enterprise.socialcv.security.Account u where u.userName = ?1");
query.setParameter(1, userName);
This works when I run the application on Tomcat, but when I run it on JBoss I get the following error:
16:31:47,639 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http-localhost-127.0.0.1-8080-1) Handler execution resulted in exception - forwarding to resolved error view: ModelAndView: reference to view with name 'dataAccessFailure'; model is {exception=org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1}: org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:301) [org.springframework.orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect.ajc$afterThrowing$org_springframework_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$18a1ac9(JpaExceptionTranslatorAspect.aj:15) [spring-aspects-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at com.tmm.enterprise.socialcv.security.dao.AccountDAO.loadAccountByUserName(AccountDAO.java:28) [classes:]
at com.tmm.enterprise.socialcv.service.AccountService.loadAccountByUserName(AccountService.java:48) [classes:]
at com.tmm.enterprise.socialcv.service.AccountService.setCredentials(AccountService.java:241) [classes:]
at com.tmm.enterprise.socialcv.controller.HomeController.signup(HomeController.java:59) [classes:]
...
Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1
at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:446) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:67) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
... 80 more
Caused by: org.hibernate.QueryParameterException: ***Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1***
at org.hibernate.engine.query.spi.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:80) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.engine.query.spi.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:86) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.internal.AbstractQueryImpl.determineType(AbstractQueryImpl.java:444) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.internal.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:416) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:440) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
... 81 more
I tried to go to both of the following queries, but no luck so far:
Query query = getEntityManager().createQuery("select u from com.tmm.enterprise.socialcv.security.Account u where u.userName = ?");
query.setParameter(0, userName);
The above is giving me the same error. How it does it:
Query query = getEntityManager().createQuery("select u from com.tmm.enterprise.socialcv.security.Account u where u.userName = ?");
query.setParameter(1, userName);
And switching to named parameters gives me the error that you cannot find the named parameter.
Any ideas? (as an aside, I also had to upgrade the DAO to a fully qualified account in the JBoss request - on tomcat, which only worked with the account request)
I have fixed an error like this this way
schema.table = ' ?' ->error
schema.table = '?' ->no error
Maybe it helps someone.
Based on Hibernate HQL reference , if you are using JDBC positional style you only need to use ?
. If you are using named parameters, you must use :paramName
.
query = getEntityManager().createQuery(
"select u from com.tmm.enterprise.socialcv.security.Account u where u.userName = :userName");
query.setParameter("userName", userName);
Even in my case nothing worked, I tried
- Positional parameter with index 0
- Positioning parameter with index 1
- Named parameter
Finally, I found that the parameter list of the processed request object was empty every time, resulting in an error when called setParameter()
.
Solution: the entity mapping was missing in the file persistence.xml
, after adding it it started working !!