Conversation
This is more complicated than the bulk load unfortunately. Previously, for update/put or their plural equivalents, we'd have to run one or two SELECT statements per entity to check if it exists already (and for Hibernate to load the object into the context to merge it with the input). Both updateAll and putAll now run, per input class, one large SELECT statement to find out which entities already exist, which also loads the entities into the persistence context (which is basically a cache). For updateAll, we require that all of the entities already exist. For putAll, we have a split path - for entities that already exist, we just call a normal merge() - for entities that don't already exist, we run merge() while instructing Hibernate that the object is transient (i.e. new). This means that we get the "put" behavior (not modifying the original object) while avoid a superfluous SELECT statement that'd check again to see if the object exists in the DB already. For new objects in putAll, we also use EntityManager::merge (rather than insert/persist) so that Hibernate creates a deep copy of the entity instead of mutating the caller's instance in place. To prevent Hibernate's DefaultMergeEventListener from firing a redundant SELECT statement when merging a known-new entity with a non-null ID, TransactionInfo implements Interceptor and overrides isTransient(Object) to return Boolean.TRUE while merging a known-transient entity. We add comments clarifying that "insert" will modify an entity in place (i.e. UpdateAutoTimestamp or generated IDs) whereas "put" will not. This has always been the case; the comments are just to clarify.
gbrodman
force-pushed
the
bulkPutAndUpdate
branch
from
September 23, 2026 14:29
dbf611e to
768c8e2
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This is more complicated than the bulk load unfortunately.
Previously, for update/put or their plural equivalents, we'd have to run
one or two SELECT statements per entity to check if it exists already
(and for Hibernate to load the object into the context to merge it with
the input).
Both updateAll and putAll now run, per input class, one large SELECT statement
to find out which entities already exist, which also loads the entities
into the persistence context (which is basically a cache). For updateAll,
we require that all of the entities already exist. For putAll, we have a
split path
Hibernate that the object is transient (i.e. new). This means that we
get the "put" behavior (not modifying the original object) while avoid a
superfluous SELECT statement that'd check again to see if the object exists in
the DB already.
For new objects in putAll, we also use EntityManager::merge (rather than
insert/persist) so that Hibernate creates a deep copy of the entity
instead of mutating the caller's instance in place. To prevent
Hibernate's DefaultMergeEventListener from firing a redundant SELECT
statement when merging a known-new entity with a non-null ID,
TransactionInfo implements Interceptor and overrides
isTransient(Object) to return Boolean.TRUE while merging a
known-transient entity.
We add comments clarifying that "insert" will modify an entity in place
(i.e. UpdateAutoTimestamp or generated IDs) whereas "put" will not. This
has always been the case; the comments are just to clarify.
This change is