Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing list to IN clause in HQL or SQL?

I get List<Strings> by executing a query. This must be passed to another query of IN clause values. How to pass them in HQL?

We can convert List to Array and can pass it, that's not a problem.

Finally, I must pass the list in List<String> or Array or String form to the IN clause.

like image 433
Mr.Chowdary Avatar asked Sep 29 '12 13:09

Mr.Chowdary


People also ask

What is difference between HQL and SQL?

SQL is based on a relational database model whereas HQL is a combination of object-oriented programming with relational database concepts. SQL manipulates data stored in tables and modifies its rows and columns. HQL is concerned about objects and its properties.

Why we use HQL instead of SQL?

Unlike SQL, HQL uses classes and properties in lieu of tables and columns. HQL supports polymorphism as well as associations, which in turn allows developers to write queries using less code as compared to SQL.


2 Answers

from AUTOS a where a.model in (select m.model from MODELS m)  

or

Query query1 = session.createQuery("select s.id from Salary s where s.salary < 50000 AND s.salary > 49980"); Query query2 = session.createQuery("from Employee e where e.id in (:ids)").setParameterList("ids", query1.list()); query2.list(); 
like image 52
Sergii Shevchyk Avatar answered Sep 24 '22 21:09

Sergii Shevchyk


I know it's been a while and you have been trying to pass the value of a different query as a queryParameter, you can also pass set or collections to in clause in HQL with 'elements()' - here's a simple example of such usage: Hibernate query: does a Set contains a certain Object?

like image 20
Sinan Kucukkoseler Avatar answered Sep 25 '22 21:09

Sinan Kucukkoseler