Right - I want to delete (e.g.) 1,000,000 records from a database. This takes a long time -> the transaction times out and fails. So - I delete them in batches say 25000 records per transaction. Using the limit clause on MySQL or ROWNUM on Oracle. Great this works.
I want to do this in a database indepedent way. And from an existing Java code base that uses JPA/Hibernate.
Out of luck. JPA Query.setMaxResults and setFirstResult have no effect for write 'queries' (e.g. delete). Selecting many entities into memory to delete them individually is very slow and dumb I'd say.
So I use a native query and manage the 'limit' clause in application code. It'd be nice to encapsulate this clause in orm.xml but ... "Hibernate Annotations 3.2 does not support bulk update/deletes using native queries." - http://opensource.atlassian.com/projects/hibernate/browse/ANN-469.
I'd imagine this is a common problem. Anybody got a better database independent solution?