将列表参数设置为本机查询 [英] Set list parameter to native query

查看:111
本文介绍了将列表参数设置为本机查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将参数设置为本地查询,

I would like to set parameter to a native query,

javax.persistence.EntityManager.createNativeQuery

类似的东西

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 ?")

是否可以为本机查询设置列表参数,而无需将其强制转换为字符串并将其附加到sql查询?

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

P.S.我正在使用EclipseLink 2.5.0和MySQL服务器5.6.13

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

谢谢

推荐答案

我相信您只能将列表参数设置为JPQL查询,而不能设置为本地查询.

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

要么使用JPQL,要么使用列表动态构建SQL.

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

这篇关于将列表参数设置为本机查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆