【问题标题】:Add 2 list in parameter in JPA using In Query使用 In Query 在 JPA 中的参数中添加 2 个列表
【发布时间】:2013-05-30 11:06:27
【问题描述】:

我想在 jpa 查询中使用 IN 查询传递两个列表

例如。 select custid from employee where (custid,name) in ((1,"ford"),(3,"eli"))

这是正常的查询,它有效,但这里我有 custid 和 name 的列表,所以我想使用 IN Query 传递列表。

查询是

String sql="select employee.custid from Employee employee

where (employee.id.custid,employee.id.name)  IN (  (:listofcustid , :listofname ))";

我将参数设置为

    Query query = em.createQuery(sql); 
    query.setParameter("listofcustid", custidlist);
    query.setParameter("listofname", namelist);

但它不起作用并给出异常

    unexpected AST node: {vector} 
[ select employee.custid from com.dao.Employee  employee where 
(employee.id.custid,employee.id.name)  IN 
((:custIdList0_, :custIdList1_, :custIdList2_),(:nameList0_, :nameList1_, :nameList2_))]

但对于单个列表,它工作正常

  String sql="select employee.custid from Employee employee

    where employee.id.custid IN ( :listofcustid )";

query.setParameter("listofcustid",listofcustid);

当我尝试移除内括号时

( :listofcustid , :listofname )

它使用 (? , ? , ? , ? , ? , ?) 中的参数提供整个精确查询的输出,

这意味着它没有将其视为 tuple ,然后是异常和以下行

" ORA-00920: invalid relational operator "

【问题讨论】:

  • 为什么不使用“where employee.id.custid in :listofcustid AND employee.id.name in :listofname”?
  • 这个效率不高,否则我们已经用过OR也
  • @IgorRodriguez:这将是一个不同的查询。第一个相当于where (custid = 1 and name = 'ford') or (custid = 3 and name = 'eli'),而你的相当于where (custid = 1 or custid = 3) and (name = 'ford' or name = 'eli')
  • 当我尝试删除内部大括号时,它给出的输出是完整的精确查询,参数为 (? , ? , ? , ? , ? , ?) ,这意味着它没有接受它作为 tuple ,后跟异常和以下行也是“ ORA-00920:无效的关系运算符”

标签: java sql list jpa jpql


【解决方案1】:

不应该是这样的吗? (每个命名参数的左括号和右括号)

String sql="select employee.custid from Employee employee

where (employee.id.custid,employee.id.name)  IN ((:listofcustid) , (:listofname ))";

【讨论】:

    猜你喜欢
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 2022-11-22
    相关资源
    最近更新 更多