2

what is the best way to delete multiple records using hibernate.My requirement is i am getting a list of UUIDs and based on this need to remove all records from the database.

Since hibernate do not have any build in delete method which can take collection as input and do the work on behalf of us.

So i am looking for the best possible way to achieve this. I am not going for bulk delete option since number of records will not gooing to be so high and nor we are using Spring's Template which has support for this type of operation.

Thanks in advance Umesh

1 Answer 1

3

Two options come to mind:

  • fetch all records and call session.delete(..) on each entity
  • use HQL delete, with a WHERE clause. But have in mind that cascades won't be handled.
Sign up to request clarification or add additional context in comments.

4 Comments

First option is already in my mind regarding second one its possible since the table where i need to delete is independent one and do not have any relation.i am thinking something session.createQuery() and than set the list as parameter not sure though if this is a right approach or not?
You could use Session.load in the first solution to avoid issuing any select query just to have an entity to delete. For the second solution, just make sure not to put too many ids in the IN clause of the query. Oracle limits them to 1000, for example.
@umesh awasthi if you don't have any relation, it is the right approach to use createQuery() with DELETE.
@JB:as per my expectations number of ids will not going to be more than 40-50 at at time (Even this is maximum)

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.