Here are the sequences leading to the question :
Im confused on :
Please enlighten me :)
Thank you !
JPA's merge method copies the state of a detached entity to a managed instance of the same entity. Hibernate, therefore, executes an SQL SELECT statement to retrieve a managed entity from the database.
Now, Hibernate can use lazy loading, which means it will load only the required classes, not all classes. It prevents a huge load since the entity is loaded only once when necessary. Lazy loading improves performance by avoiding unnecessary computation and reduce memory requirements.
To enable lazy loading explicitly you must use “fetch = FetchType. LAZY” on an association that you want to lazy load when you are using hibernate annotations.
JPA Specification says:
The semantics of the merge operation applied to an entity X are as follows:
If X is a detached entity, the state of X is copied onto a pre-existing managed entity instance X' of the same identity or a new managed copy X' of X is created.
If X is a new entity instance, a new managed entity instance X' is created and the state of X is copied into the new managed entity instance X'.
If X is a removed entity instance, an
IllegalArgumentExceptionwill be thrown by the merge operation (or the transaction commit will fail).If X is a managed entity, it is ignored by the merge operation, however, the merge operation is cascaded to entities referenced by relationships from X if these relationships have been annotated with the cascade element value
cascade=MERGEorcascade=ALLannotation.For all entities Y referenced by relationships from X having the cascade element value
cascade=MERGEorcascade=ALL, Y is merged recursively as Y'. For all such Y referenced by X, X' is set to reference Y'. (Note that if X is managed then X is the same object as X'.)If X is an entity merged to X', with a reference to another entity Y, where
cascade=MERGEorcascade=ALLis not specified, then navigation of the same association from X' yields a reference to a managed object Y' with the same persistent identity as Y.
As you can see, there is no magic here. The state of detached instance is copied into the newly created managed instance. Since your detached instance has an empty list, managed instance would have it too.
Further behaviour depends on ownership of relationship, since representation in the database reflects the owning side of relationship:
Team is the owning side, relationships between Team and Players will be destroyed during flush (but Player itself would survive unless you have orphanRemoval = true on your relationship).Team doesn't affect the database.If you refresh the Team before flushing the context, all properties of Team are rewritten by values from the database, therefore list of Players is restored (since the empty list of players wasn't flushed yet).
If you call flush() before calling refresh(), and Team is the owning side, list will be empty, since destruction of relationships was propagated to the database during flush().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With