Hibernate Native Query problem with named parameters

后端 未结 4 1253
后悔当初
后悔当初 2020-12-16 22:12

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 followin

相关标签:
4条回答
  • 2020-12-16 22:45

    MyInterceptor extends EmptyInterceptor works as mentioned by cherouvim.

    don't forget

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

    in persistence.xml

    0 讨论(0)
  • 2020-12-16 22:57

    The colon is not escapeable in Hibernate itself (known Bug since 2005).

    0 讨论(0)
  • 2020-12-16 23:04

    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.

    0 讨论(0)
  • 2020-12-16 23:05
    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.

    0 讨论(0)
提交回复
热议问题