问题
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