当前位置: 首页 > 知识库问答 >
问题:

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

马野
2023-03-14

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

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

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上,它只是在查询帐户)

共有3个答案

严兴言
2023-03-14
query = getEntityManager().createQuery(
"select u from com.tmm.enterprise.socialcv.security.Account u where u.userName = :userName");
query.setParameter("userName", userName);
陈昂熙
2023-03-14

基于Hibernate HQL参考,如果您使用JDBC位置样式,您只需要使用<代码> 。如果您使用的是命名参数,则应使用:paramName。

双恩
2023-03-14

我用那种方法纠正了这样的错误

schema.table = ' ?' ->error

schema.table = '?' ->no error

也许这对某人有帮助。

 类似资料: