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

和自甴很熟 提交于 2020-01-05 08:43:04

问题


I have a list of different ids and their names. For every id[0] we have name[0] that needs to be matched.

  • list of ids, l{1,2,3,4};
  • list of names, 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?

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:

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

Any suggestions?


回答1:


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. 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].

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:

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

Then you can in hql use it as a normal field. like ... from table where idNamePair in (:paramList)



来源:https://stackoverflow.com/questions/41036036/how-to-check-if-a-pair-exists-using-hql-in-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!