0

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.

2 Answers 2

1

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);
 }
Sign up to request clarification or add additional context in comments.

Comments

1

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'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.