sql - How to use a dynamic parameter in a IN clause of a JPA named query? -


my problem kind of query :

select * sometable somefield in ('string1','string2'); 

the previous code works fine within sql developer. same static query works fine , returns me few results;

query nativequery = em.createnativequery(thepreviousquery,new someresultset()); return nativequery.getresultlist(); 

but when try parameterize this, encounter problem.

final string parameterizedquery = "select * sometable somefield in (?selectedvalues)"; query nativequery = em.createnativequery(parameterizedquery ,new someresultset()); nativequery.setparameter("selectedvalues","'string1','string2'"); return nativequery.getresultlist(); 

i got no result (but no error in console). , when @ log, see such thing :

select * sometable somefield in (?) bind => [string1,string2] 

i tried use no quotes (with similar result), or non ordered parameter (:selectedvalues), leads such error :

sql error: missing in or out parameter @ index:: 1 

i enventually tried had parentheses set directly in parameter, instead of query, didn't work either...

i build query @ runtime, match first (working) case, i'd rather proper way; thus, if has idea, i'll read them great interest!

fyi : jpa version 1.0 oracle 11g

jpa support use of collection list literal parameter in jpql queries, not in native queries. jpa providers support proprietary feature, it's not part of jpa specification (see https://stackoverflow.com/a/3145275/1285097).

named parameters in native queries aren't part of jpa specification. behavior depends on persistence provider and/or jdbc driver.

hibernate jdbc driver oracle support both of these features.

list<string> selectedvalues = arrays.aslist("string1", "string2"); final string parameterizedquery =      "select * sometable somefield in (:selectedvalues)"; return em.createnativequery(parameterizedquery)         .setparameter("selectedvalues", selectedvalues)         .getresultlist(); 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -