java - Spring JPA - How to force custom order of repository calls within a transaction -


using spring data jpa 1.11.6.release, have following trouble;

with simple 1-to-n relationship between them, have tablea, , tableb, a contains multiple b. , have repository of tableb custom delete method;

public interface brepository extends jparepository<tableb, string> {     @modifying     void deletebytablea(tablea tablea); } 

where deletes fk value, pk of tablea

in service class, use these;

@service public class theservice {      @autowired     private arepository arepository;     @autowired     private brepository brepository;      @transactional     public void deleteinsert(list<tableb> blist) {         tablea tablea = arepository.findbyetc(...);         brepository.deletebytablea(tablea);         brepository.save(blist);     } } 

but issue order of operations inside @transactional method changes according optimization done hibernate. causing unique constraint violations, in case have duplicate entities of tableb exist in new table. if put brepository.count() call between delete & save calls, forces custom order, i'd achieve configuration rather such silly tricks. there way achieve this?

it seems answer problem lies in hibernate docs order of flush operation. according hibernate javadocs sql operations order is:

  • inserts
  • updates
  • deletions of collections elements
  • inserts of collection elements
  • deletes

you may want flush manually after first operation:

@transactional public void deleteinsert(list<tableb> blist) {     tablea tablea = arepository.findbyetc(...);     brepository.deletebytablea(tablea);     brepository.flush();      brepository.save(blist); } 

or try replace "delete + insert" operation update operation.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -