简体   繁体   中英

Hibernate Native Query problem with named parameters

I have a problem with Hibernate Native Query. I have one SELECT that selects array slice (PostgreSQL database).

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.

I tried to escape the colon (:) with ':' , '::' but with no success.

The Hibernate version is 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) .

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.

Edit : I'm not 100% sure the above works (rewriting native SQL and whether the query parser would allow the special character). I've only done the above in HQL and criteria where I was passing the index hint as a query comment.

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

MyInterceptor extends EmptyInterceptor works as mentioned by cherouvim.

don't forget

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

in 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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