将Hibernate应用程序移动到JBoss - 超出声明的序数参数的位置

时间:2012-04-09 15:42:40

标签: java hibernate jboss

我有一个正在使用tomcat / CloudFoundry的Hibernate Web应用程序,但是当我尝试在JBoss上运行它时,我遇到了一些错误。

在我的DAO中,我创建了一个按userName加载用户的查询,如下所示:

Query query = getEntityManager().createQuery("select u from com.tmm.enterprise.socialcv.security.Account u where u.userName = ?1");
query.setParameter(1, userName);

当我在Tomcat上运行应用程序时,上面的工作正常,但是当我在JBoss上运行它时,我收到以下错误:

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

我已尝试更改以下两个查询,但仍然没有运气:

Query query = getEntityManager().createQuery("select u from com.tmm.enterprise.socialcv.security.Account u where u.userName = ?");
        query.setParameter(0, userName);

上面给出了同样的错误。同样如下:

Query query = getEntityManager().createQuery("select u from com.tmm.enterprise.socialcv.security.Account u where u.userName = ?");
        query.setParameter(1, userName);

切换到命名参数会给我一个关于无法找到命名参数的错误。

有什么想法吗? (顺便说一句,我还必须在JBoss的查询中将DAO更新为完全限定的帐户 - 在tomcat上它只是在查询帐户)

4 个答案:

答案 0 :(得分:1)

我以这种方式解决了这样的错误

schema.table = ' ?' ->error

schema.table = '?' ->no error

也许对某人有所帮助。

答案 1 :(得分:0)

基于Hibernate HQL reference,如果使用JDBC位置样式,则必须使用?。如果您使用的是命名参数,则应使用:paramName

答案 2 :(得分:0)

query = getEntityManager().createQuery(
"select u from com.tmm.enterprise.socialcv.security.Account u where u.userName = :userName");
query.setParameter("userName", userName);

答案 3 :(得分:0)

即使在我的情况下,没有任何工作,我已经尝试了

  1. 索引为0的位置参数
  2. 索引为1的位置参数
  3. 命名参数
  4. 最后,我发现每次调用hashCode时,生成的查询对象的参数列表都是空的。

    解决方案: window.jQuery.Velocity 文件中缺少实体映射,添加后,它开始工作!!