1

I have some trivial table in database (let say Oracle10g) and I need to implement at DAO ability to delete multiple records. The method remove() receives as a parameter an array of ids (integers).

For now I have a query string "DELETE FROM news WHERE id = ?" which I use at PreparedStatement. I simply add batch for every id from array and then perform execute on PreparedStatement.

I wonder if there any ability to perform it through one query statement, something like "DELETE FROM news WHERE id IN ?". But I cannot find how to properly set an array of integers instead of '?'.

The same question applies to Hibernate and JPA. If there any constructions to solve this ? Because now I use batch-like-way: add Query to Session on every id from array and commit transaction.

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Dmitry Kankalovich
  • 311
  • 1
  • 6
  • 18
  • See this answer:http://stackoverflow.com/a/189399/851432. It points to a [link](http://www.javaranch.com/journal/200510/Journal200510.jsp#a2) which may be useful in this context. – Jomoos Dec 07 '11 at 05:52

1 Answers1

4

The best I've seen done is to dynamically build the String used by the PreparedStatement, inserting the proper # of ?, sequences, then use a for loop to call setInt as appropriate for each row - each row to be deleted in your case.

JPA provides a special syntax for this (can accept a Collection to populate the list of arguments), since it has to create the SQL anyway - and likely does so very similar to how I just described. Specifics as to the API calls (for both JPA and HQL) are available at Hibernate HQL Query : How to set a Collection as a named parameter of a Query? .

Community
  • 1
  • 1
ziesemer
  • 26,239
  • 8
  • 80
  • 90