mirror of
https://github.com/google/nomulus
synced 2026-10-21 22:02:44 +00:00
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.