Follow up #3173 - Change BeanSet clear() with orphanRemoval to lazy load ALL properties (avoid 1+N) #3205
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Scenario:
set.clear() on a Set with orphanRemoval = true
Currently the clear() invokes lazy loads only fetching the Id properties but this is problematic when the entity bean in the Set has a hashcode/equals implementation as it results in 1 + N queries. That is, for orphan removal it is identifying the beans that are going to be deleted and when doing that (with only the Id property fetched) the hashcode/equals implementation is invoked and that then invokes lazy loading N times where N is the number of elements in the collection that is being cleared.
To avoid the 1+N lazy loading queries instead the clear() invokes lazy loading that includes ALL the properties such that equals/hashcode implementation does not itself invoke lazy loading.
To be awesome, ebean would detect the properties that are used in hashcode/equals implementation and only fetch the Id property and those properties involved in the hashcode/equals for this case (rather than all properties). This might be a future optimisation that we could pursue using the entity bean enhancement to detect those properties used in the hashcode/equals.