Hibernate HQL: pass pair values as parameters in IN clause

白昼怎懂夜的黑 提交于 2019-12-23 18:27:57

问题


I am facing a problem: how to pass parameters to HQL for pair values in query with IN clause like - select id, name from ABC where (id, reg_date) in ('x', 'y'). And the parameters are of different data types string (id) and reg_date (date). Now, in HQL I can write:

hql = "select id, Name from ABC where (id, date) in (:listparam1)"
hibernateTemplate.findbynamedparam(hql, "listparam1", values).

Problem: how to pass a list of id and date pairs in HQL? Both parameters are of different data types: one is String and the other is Timestamp. I tried Map and concatenation of both values in Object, 2 dimensional array, but nothing is working.


回答1:


select a.id, a.name from ABC a where a.id || '.' || a.date in (:id_date)

....and you need to pass as parameter "id_date" collection of string of concatenated id and name. If performance is an obstacle, you can create function index on your SQL database(s) that do exactly the same job (of course if your database(s) supports function indexes)




回答2:


You can implement it next way:

hql = "select a.id, a.Name from ABC a where a.id in (:listid) or a.date in (:datelist)"

or

 hql = "select a.id, a.Name from ABC a where a.id in (:listid) and a.date in (:datelist)"

regardless of what condition must be satisfied with the result



来源:https://stackoverflow.com/questions/28284197/hibernate-hql-pass-pair-values-as-parameters-in-in-clause

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