| In the same PR, I've added a fix that removes the EntityKey for the entities that are not found by the batch query. Here is what happens now. As the query results are processed, the entities keys for entities with the following IDs are added to BatchFetchQueue#batchLoadableEntityKeys: [0,1,2,3,4,5,6,7] When processing associated entity with id == 0:
- batch query is select ... from Task batchfetch0_ where batchfetch0_.id in (?, ?, ?, ?, ?)
- batch query is executed with IDs bound to query: [0,1,2,3,4};
- none of the entities are found;
- associated entity with id == 0 is set to null.
BatchFetchQueue#batchLoadableEntityKeys still contains EntityKey for entities with IDs [5,6,7]. When processing associated entity with id == 1:
- batch query (with 4 parameters) is select ... from Task batchfetch0_ where batchfetch0_.id in (?, ?, ?, ?)
- batch query is executed with IDs bound to query: [1,5,6,7];
- associated entity with id == 7 was found, put in the PersistenceContext; none of the others are found;
- associated entity with id == 1 is set to null.
BatchFetchQueue#batchLoadableEntityKeys is empty. A simple load query is used when processing the rest of the entities. select ... from Task batchfetch0_ where batchfetch0_.id=? When processing associated entity with id == 2:
- load query for Task with id == 2 is executed;
- associated entity with id == 2 is set to null.
When processing associated entity with id == 3:
- load query for Task with id == 3 is executed;
- associated entity with id == 3 is set to null.
When processing associated entity with id == 4:
- load query for Task with id == 4 is executed;
- associated entity with id == 4 is set to null.
When processing associated entity with id == 5:
- load query for Task with id == 5 is executed;
- associated entity with id == 5 is set to null.
When processing associated entity with id == 6:
- load query for Task with id == 6 is executed;
- associated entity with id == 6 is set to null.
When processing associated entity with id == 7:
- the associated entity with id == 7 is found in the PersistenceContext, so no extra query executed.
This fix should perform much better. Once the batch is empty, it still requires doing an extra load query for those entities that were not found. [1] https://github.com/hibernate/hibernate-orm/pull/2014 |