Answer the question
In order to leave comments, you need to log in
[spring jpa repository] how to deleteInBatch correctly?
Hello, it is
necessary to delete records from the table and create new ones, new ones can have the same primary keys as the deleted ones. Deleted by foreign key.
If you do this, then JPA / hibernet first makes a request to the database, then deletes the records and performs a DELETE FROM for each. Which is not very optimal, since there can be a lot of records.
rep.deleteAllByForeignKey(forejgnKey);
inputList.forEach(newrec -> repo.save(newrec));
repo.deleteInBatch(repo.findAllByForeignKey(foreignKey));
inputList.forEach(newrec -> repo.save(newrec));
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question