Hibernate的位置参数基于零 [英] Hibernate positional parameters zero based

查看:437
本文介绍了Hibernate的位置参数基于零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我注意到,对于Hibernate 5.2.0,原生查询现在需要基于零参数定位。

根据 JPA 2.1规范


3.10.13位置参数



除了定义了已命名参数的存储过程查询外,只有位置参数绑定和结果项的位置访问才可以移植用于本地
查询。
绑定位置参数的值时,编号开始为1。
假定对于本地
查询,参数本身使用SQL语法(即? ,而不是?1)。

我对规范的理解是,即使对于本机查询,编号也应该以1。

现在根据Hibernate文档 Query.setParameter(int,Object)
位置编号从0开始。在Hibernate 4.2和5.2的文档中。



我做了一个微测试



首先使用Hibernate 4.2.6



  @PersistenceContext 
private EntityManager entityManager;


查询查询= entityManager.createNativeQuery(select * from Game g where title =?);
query.setParameter(1,GAME_TITLES [0]);
List list = query.getResultList();

这适用于hibernate 4.2.6。



 < persistence-unit name =testtransaction-type =JTA > 
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< jta-data-source> jdbc / arquillian< / jta-data-source>
<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.H2Dialect/>
< property name =hibernate.hbm2ddl.autovalue =create-drop/>
< / properties>
< / persistence-unit>



与Hibernate 5.2一样



  Query query = entityManager.createNativeQuery(select * from game g where title =?); 
query.setParameter(0,GAME_TITLES [0]);
List list = query.getResultList();

唯一的区别是setParameter中的0索引。



persistence.xml也非常相似。

 < persistence-unit name =test> 
< provider> org.hibernate.jpa.HibernatePersistenceProvider< / provider>
< jta-data-source> jdbc / arquillian< / jta-data-source>
<属性>
< property name =hibernate.transaction.jta.platformvalue =org.hibernate.service.jta.platform.internal.SunOneJtaPlatform/>
< property name =hibernate.hbm2ddl.autovalue =create-drop/>
< / properties>
< / persistence-unit>

我追踪了两个版本中的代码。我可以在4.2.6找到基于1的索引。我在5.2版找不到类似的代码。



我在hibernate 论坛追溯到2009年:


好吧,只有当您使用JPA-Query-Api时,第一个参数必须有index = 1。
您正在使用Hibernate-Query-Api第一个参数必须有index = 0。

Obvioulsy我正在使用JPA。
所以问题是:

有没有办法配置Hibernate 5.2来取回基于1的位置参数?我不愿意改变代码以符合规范。

解决方案

Hibernate 5.2合并了 hibernate-entitymanager 模块到 hibernate-core ,所以这个问题可能发生在这个过程中。



您需要打开JIRA问题,我们会处理它。


$ b Hibernate 5.2.1 修正这个问题。


I'm migrating a project from Hibernate 4.2.6 to 5.2.0.

I noticed that for Hibernate 5.2.0, the native queries now require zero based parameter positionning.

According to the JPA 2.1 Specification

3.10.13 Positional Parameters

Only positional parameter binding and positional access to result items may be portably used for native queries, except for stored procedure queries for which named parameters have been defined. When binding the values of positional parameters, the numbering starts as "1". It is assumed that for native queries the parameters themselves use the SQL syntax (i.e., "?", rather than "?1").

My understanding of the specification is that even for native queries, the numbering should start with 1.

Now according to Hibernate documentation of Query.setParameter(int, Object). The position is numbered from 0. In the documentation for Hibernate 4.2 as well as for 5.2.

I made a micro test

First with Hibernate 4.2.6

@PersistenceContext 
private EntityManager entityManager;


Query query = entityManager.createNativeQuery("select * from Game g where title = ?");
query.setParameter(1, GAME_TITLES[0]);
List list = query.getResultList();

This works with hibernate 4.2.6.

The persistence.xml file looks like this

<persistence-unit name="test" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/arquillian</jta-data-source>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    </properties>
</persistence-unit>

The same with Hibernate 5.2

Query query = entityManager.createNativeQuery("select * from Game g where title = ?");
query.setParameter(0, GAME_TITLES[0]);
List list = query.getResultList();

The only difference is the 0 index in the setParameter.

The persistence.xml is also very similar

<persistence-unit name="test">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>jdbc/arquillian</jta-data-source>
    <properties>
        <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    </properties>
</persistence-unit>

I traced the code in both version. I could find in 4.2.6 where the 1 based index is handeled. I could not find a similar code in version 5.2.

I found a post in the hibernate forums dating back to 2009 that:

Well, only if you use the JPA-Query-Api the first parameter must have index = 1. You are using the Hibernate-Query-Api where the first parameter must have index = 0.

Obvioulsy I'm using JPA. So the question is:

Is there a way to configure Hibernate 5.2 to get back the 1 based positional parameter? I would hate to change the code in order not to conform to the specification.

解决方案

Hibernate 5.2 has merged the hibernate-entitymanager module into hibernate-core, so this issue might have occurred in this process.

You need to open a JIRA issue, and we'll take care of it.

Hibernate 5.2.1 fixes this issue.

这篇关于Hibernate的位置参数基于零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆