gpt4 book ai didi

java - 为什么会发生 "Could not locate ordinal parameter [0], expecting one of []"?

转载 作者:行者123 更新时间:2023-12-05 00:53:02 28 4
gpt4 key购买 nike

我使用 Hibernate 作为 ORM。当我搜索这个错误时,据说我的问题是由于映射,但我检查了它并没有发现任何问题

我的查询是:

select result from com.a.b.c.loan.ArchiveHistory result where result.id =?

映射是:

<hibernate-mapping>
<class name="com.a.b.c.loan.ArchiveHistory" mutable="false" table="ARCT_HISTORY_VIEW">

<id name="id" column="BIBLIOGRAPHICID" type="long" unsaved-value="0">
</id>
</class>
</hibernate-mapping>

最佳答案

正如 documentation 中所述:

JPQL-style positional parameters are declared using a question mark followed by an ordinal - ?1, ?2. The ordinals start with 1. Just like with named parameters, positional parameters can also appear multiple times in a query.

因此,尝试以这种方式重写您的查询:

Query query = entityManager.createQuery(
"select result from com.a.b.c.loan.ArchiveHistory result where result.id =?1"
).setParameter( 1, 23L );

或者,作为更具可读性的替代方案,我建议使用名称绑定(bind)参数:

Query query = entityManager.createQuery(
"select result from com.a.b.c.loan.ArchiveHistory result where result.id = :id"
).setParameter("id", 23L );

关于java - 为什么会发生 "Could not locate ordinal parameter [0], expecting one of []"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68619839/

28 4 0