Deleting entities is not happening in the right order. According to documentation: _all entity deletions shall happen in the same order the corresponding objects were deleted using Session.delete()_
When property, _hibernate.jdbc.batch_size = 20_, hibernate is using BatchingBatch, which at line 86, decides whether the statements shall be executed or not: _if ( batchPosition == batchSize ) {...}_
*Given* FOO and BAR entities both with InheritanceType.JOINED and FOO having a ManyToOne relation to BAR
*When* The following two lines are executed: entityManager.remove(foo); entityManager.remove(bar);
*Then* Hibernate doesn't execute the statement to remove FOO, but later It does execute the statement to remove BAR And as a result: _delete from BAR where id=2_ *throws* _Referential integrity constraint violation: "FKJFU6P0IRDHEYXGG5UDT0EAJAH: PUBLIC.FOO FOREIGN KEY(BAR_ID) REFERENCES PUBLIC.BAR(ID) (2)";_
Please check the attached test application. Just run _mvn install_.
I guess, BatchingBatch should keep track of the order in which the objects where deleted through Session.delete() and when one of the batches has reached the limit or before a non-batch statement is ready to executed, all other batches shall be executed as well.
P.S. The project has the P6Spy configured together with hibernate show sql. With the new batching feature in place, the latter is lying: {code:java} Hibernate: delete from FOO where id=? // This is added to batch and not executed Hibernate: delete from BAR where id=? // This one is executed, as you see from P6Spy output, but fails |2|statement|connection 3|delete from BAR where id=?|delete from BAR where id=2 Apr 11, 2018 5:02:08 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions {code} |
|