简体   繁体   English

带有命名参数的Hibernate Native Query问题

[英]Hibernate Native Query problem with named parameters

I have a problem with Hibernate Native Query. 我有Hibernate Native Query的问题。 I have one SELECT that selects array slice (PostgreSQL database). 我有一个选择数组切片的SELECT(PostgreSQL数据库)。

The problem is that hibernate recognizes the following part: ":300" from "SELECT my_array[1:300]..." as named parameter and I get the following exception: Not all named parameters have been set. 问题是hibernate识别以下部分:“SELECT my_array [1:300] ...”中的“:300”作为命名参数,我得到以下异常:并未设置所有命名参数。

I tried to escape the colon (:) with ':' , '::' but with no success. 我试图用':','::'来逃避冒号(:),但没有成功。

The Hibernate version is 3.2 Hibernate版本是3.2

I don't use PostgreSQL but if you don't find a proper solution for this problem you can implement an interceptor (extend EmptyInterceptor) and modify your query on onPrepareStatement(String sql) . 我不使用PostgreSQL但是如果你没有找到适合这个问题的解决方案,你可以实现一个拦截器(扩展EmptyInterceptor)并修改onPrepareStatement(String sql)上的查询。

Which means that you could be using something like my_array[1|300] and rewriting it as my_array[1:300] to workaround the named parameters problem. 这意味着您可以使用类似my_array[1|300]并将其重写为my_array[1:300]以解决命名参数问题。

Edit : I'm not 100% sure the above works (rewriting native SQL and whether the query parser would allow the special character). 编辑 :我不是100%确定以上工作(重写本机SQL以及查询解析器是否允许特殊字符)。 I've only done the above in HQL and criteria where I was passing the index hint as a query comment. 我只在HQL和标准中完成了上述操作,我将索引提示作为查询注释传递。

在Hibernate本身中,冒号无法逃脱(自2005年以来已知的Bug )。

MyInterceptor extends EmptyInterceptor works as mentioned by cherouvim. MyInterceptor extends EmptyInterceptor工作,如cherouvim所述。

don't forget 别忘了

<property name="hibernate.ejb.interceptor" value="MyInterceptor"/>

in persistence.xml 在persistence.xml中

create function array_slice(a anyarray, start int4, end int4) returns anyarray as   
$$
    SELECT a[start:end];
$$
language(sql);

Now call this function instead. 现在调用此函数。 Did not try it but it will work somehow like this. 没试过,但它会以某种方式工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM