【问题标题】:How to check if a pair exists using HQL in query?如何在查询中使用 HQL 检查一对是否存在?
【发布时间】:2016-12-08 09:33:37
【问题描述】:

我有一个不同 ID 及其名称的列表。对于每个id[0],我们都有需要匹配的name[0]

  • ID 列表,l{1,2,3,4};
  • 名称列表,n{a,b,c,d};

现在假设如果我想获得上述两种组合的完全匹配,HQL 中有什么方法可以得到结果吗?

我正在寻找替代查询,例如:

select any_column 
from table_name 
where (id[0]=1 and name[0]=a) or (id[1]=2 and name[1]=b and so on...);

HQL 查询应该如下所示:

select any_column 
from table_name 
where (id,name) IN {(id[0],name[0]), (id[1], name[1]),...};

有什么建议吗?

【问题讨论】:

    标签: java hql


    【解决方案1】:

    我不是 hql/sql 人,但是我能想到的一种方法是,在您的 hql 中,您将 id 和 name 与空格(或其他特殊字符)连接,然后与 in 子句连接。类似:

    select * from table where concat(id, ' ', name) in (:pairList)
    

    pairList参数是一个java集合,需要在hql查询调用之前准备好,它有元素id[x] + " " + name[x]

    我认为这应该可行。

    如果您使用的是hibernate,另一种可能的解决方案是,在您的table 实体上使用hibernate 的@Formular 注释来创建计算列,例如:

    @Formula(value = " concat(id, ' ', name) ")
    private String idNamePair;
    

    然后你就可以在hql中把它当作一个普通的字段了。喜欢... from table where idNamePair in (:paramList)

    【讨论】:

      猜你喜欢
      • 2011-05-06
      • 2019-08-29
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多