简体   繁体   English

如何在查询中检查是否存在使用HQL的对?

[英]How to check if a pair exists using HQL in query?

I have a list of different ids and their names. 我列出了不同的ID及其名称。 For every id[0] we have name[0] that needs to be matched. 对于每个id[0]我们都需要匹配name[0]

  • list of ids, l{1,2,3,4}; ids列表, l{1,2,3,4};
  • list of names, n{a,b,c,d}; 名单, n{a,b,c,d};

Now suppose if I want to get an exact match for both above combination, is there any way in HQL to get the result? 现在假设我想要获得上述两种组合的完全匹配,是否有任何方法可以在HQL中获得结果?

I am looking to find a replacement for a query like: 我希望找到一个像以下查询的替代品:

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...);

The HQL query should be something like below: HQL查询应如下所示:

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

Any suggestions? 有什么建议么?

I am not hql/sql guy, however one way I can think of is, in your hql, you concatenate the id and name with a space (or other special char) then with in sub-clause. 我不是HQL / SQL的家伙,但我能想到的是单向的,在你的HQL,您连接ID和名称用空格(或其他特殊字符),然后用in子条款。 something like: 就像是:

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

The pairList parameter is a java collection, you should prepare before the hql query call, which has element id[x] + " " + name[x] . pairList参数是一个java集合,你应该在hql查询调用之前准备,它有元素id[x] + " " + name[x]

I think this should work. 我认为这应该有效。

If you are using hibernate, another possible solution is, make use of the hibernate's @Formular annotation on your table entity, to create a calculated column, such as: 如果你正在使用hibernate,另一种可能的解决方案是,在你的table实体上使用hibernate的@Formular注释来创建一个计算列,例如:

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

Then you can in hql use it as a normal field. 然后你可以在hql中将它用作普通字段。 like ... from table where idNamePair in (:paramList) 喜欢... from table where idNamePair in (:paramList)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM