Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way of writing a HQL in ( ... ) query

Tags:

hibernate

hql

Assuming that I want to write the following HQL query:

FROM Cat c WHERE c.id IN (1,2,3) 

what is the proper way of writing this as a parametrized query, e.g.

FROM Cat c WHERE c.id IN (?) 
like image 558
Robert Munteanu Avatar asked Jun 07 '09 12:06

Robert Munteanu


People also ask

What is HQL Hibernate Query Language )?

Hibernate Query Language (HQL) is an easy to learn and powerful query language designed as an object-oriented extension to SQL that bridges the gap between the object-oriented systems and relational databases. The HQL syntax is very similar to the SQL syntax.

What is HQL query in Java?

Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into conventional SQL queries, which in turns perform action on database.

How many ways we can write query in Hibernate?

Hibernate provides three different ways to retrieve data from a database.


1 Answers

I am unsure how to do this with positional parameter, but if you can use named parameters instead of positional, then named parameter can be placed inside brackets and setParameterList method from Query interface can be used to bind the list of values to this parameter.

... Query query = session.createQuery("FROM Cat c WHERE c.id IN (:ids)"); query.setParameterList("ids", listOfIds); ... 
like image 86
Matej Avatar answered Oct 13 '22 00:10

Matej