0

Using Spring Boot + Hibernate, I'm trying to bulk delete data using a nameQuery or nativeQuery. A few elements have constraint violations and Spring/Hibernate generates an entire rollback.

Is it possible to avoid the rollback? i.e. 100 records for deletion, 3 have constraints, so Hibernate generates a rollback for 100, but I need to rollback only 3 and still want to delete 97 elements. Right now I created a bad solution, I used a for loop and execute 100 sql queries.

On service layer:

@Transactional(noRollbackFor = { 
        SQLIntegrityConstraintViolationException.class,
        PersistenceException.class,
        ConstraintViolationException.class, 
        DataIntegrityViolationException.class })
public int deleteAll(String ids[]) {
    call.dao.delete(ids)
}

On Hibernate layer:

delete(String ids[]) {
   sql = "DELETE from mae1000 as x WHERE x.idccty in (:ids)";

   int r = getCurrentSession().createNativeQuery(sql).setParameter("ids", Arrays.asList(ids)).executeUpdate();
}       
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • It is not a matter of hibernate. The problem is your `IN query`. Give up `IN query` or `Strict constraint` – dgregory Apr 01 '20 at 07:16
  • the query works for any records without db constraint, If I put the same sql in the db console I got an answer like 'X records delete fromY', I need do the same but with hibenate, I need a commit for the right recorts. – eduardourra Apr 01 '20 at 17:33
  • Then please update your try and error from hibernate. But still, I doubt it is a matter of hibernate. – dgregory Apr 06 '20 at 09:16

0 Answers0