Tdev John
Tdev John

Reputation: 61

Hibernate 4.1.11 hql delete query with where in list

I want to delete from a table some entries with specific ids. I want to know the syntax of this request

delete from table where id in list_of_ids

in hql.

Upvotes: 6

Views: 9235

Answers (3)

Assuming the id of your table is of type Long, the correct way to perform the delete is as follows:

List<Long> ids = new ArrayList<Long>();
ids.add(1L);
ids.add(2L);

Query q = session.createQuery("DELETE FROM YourEntityName ye WHERE ye.id IN (:list)");

q.setParameterList("list", ids);
q.executeUpdate();

Upvotes: 8

Abhijith Nagarajan
Abhijith Nagarajan

Reputation: 4030

Use this

Query qry = session.createQuery(delete from Pojo where pojo.property in (:bindParameter);
qry.setParameter("bindParameter", list);

Note this statement SQL exception if list is empty.

Upvotes: 0

kark
kark

Reputation: 4853

What about delete from Table as table where id="value"

In above case

Table- Pojo class

table- Database table name


Upvotes: 0

Related Questions