Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set list parameter to native query

I would like to set parameter to a native query,

javax.persistence.EntityManager.createNativeQuery

Something like that

Query query = em.createNativeQuery("SELECT * FROM TABLE_A a WHERE a.name IN ?");
List<String> paramList = new ArrayList<String>();
paramList.add("firstValue");
paramList.add("secondValue");
query.setParameter(1, paramList);

Trying this query result in Exception:

Caused by: org.eclipse.persistence.exceptions.DatabaseException:
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
You have  an error in your SQL syntax; check the manual that corresponds to your MySQL server 
version for the right syntax to use near
'_binary'??\0♣sr\0‼java.util.ArrayListx??↔??a?♥\0☺I\0♦sizexp\0\0\0☻w♦\0\0\0t\0
f' at line 1
Error Code: 1064
Call: SELECT * FROM Client a WHERE a.name IN ?
        bind => [[firstValue, secondValue]]
Query: ReadAllQuery(referenceClass=TABLE_A sql="SELECT * FROM TABLE_A a WHERE a.name IN ?")

Is it any way to set list parameter for native query, without cast to string and append it to sql query?

P.S. I'm use EclipseLink 2.5.0 and MySQL server 5.6.13

Thanks

like image 274
Silence Avatar asked Sep 09 '13 15:09

Silence


People also ask

What is the difference between JPQL and native query?

JPQL is the most commonly used query language with JPA and Hibernate. It provides an easy way to query data from the database. But it supports only a small subset of the SQL standard, and it also does not support database-specific features. If you want to use any of these features, you need to use a native SQL query.

How do I use the same parameter multiple times in my query using JPA?

If for some reason we do need to use the same parameter many times within the same query, we just need to set it once by issuing the “setParameter” method. At runtime, the specified values will replace each occurrence of the parameter.

What is setParameter query?

The Query. setParameter(integer position, Object value) method is used to set the parameter values. Input parameters are numbered starting from 1.


1 Answers

I believe you can only set list parameters to JPQL queries, not native queries.

Either use JPQL, or construct the SQL dynamically with the list.

like image 160
James Avatar answered Oct 08 '22 15:10

James