Batching statements fails for delete

Description

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:

Environment

Java 1.8, H2, Postgres

Status

Assignee

Vlad Mihalcea

Reporter

Dumitru Boldureanu

Fix versions

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Worked in

5.2.14

Components

Affects versions

Priority

Major
Configure