Reputation: 6892
I have a sql query
delete from response where r_id in (1,2,3,4) and u_id='10';
Now I want to execute this queru using hibernate.
I know i can do this through createSQLQuery
but I have to use criteria.
How i can do this?
Thanks.
Upvotes: 0
Views: 1132
Reputation: 68715
You need to add two restrictions, one for IN, here is the java api for Restriction.in
static Criterion in(String propertyName, Object[] values) Apply an "in" constraint to the named property
So your code should look like this
Criteria criteria = session.createCriteria(Yourclass.class);
// create a collection for IN values comparision and lets assume it is called CollectionofValues
criteria.add(Restrictions.in(r_id, collectionofValues);
criteria.add(Restriction.eq(u_id,'10'));
Upvotes: 1
Reputation: 9162
If you have to use criteria here, you will have to fetch all the objects to Java and delete them using Java mechanisms.
This approach is very slow in terms of performance, but IFF you really must use it:
List<Response> doomedResponses = createCriteria(Response.class).addRestriction(/* restrictions here*/).list();
for(Response doomed : doomedResponses) {
entityManager.remove(doomed);
}
Upvotes: 1