You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/src/main/asciidoc/introduction/Interacting.adoc
+40-6Lines changed: 40 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -643,23 +643,57 @@ Notice that this code fragment is completely typesafe, again thanks to <<metamod
643
643
[[controlling-merge]]
644
644
=== Controlling state retrieval during merge
645
645
646
-
When <<persistence-operations,`merge()`>> is used with cascading, that is, when the `merge()` operation is applied to a root entity with associations mapped `cascade=MERGE`, Hibernate issues a single `select` statement to retrieve the current database state associated with the entity and its associated entities.
647
-
In certain circumstances, this query might be suboptimal.
648
-
However, this query does not occur if the root entity was already loaded into the persistence context before `merge()` is called.
646
+
The <<persistence-operations,`merge()`>> operation is usually used when some interaction with a user or automated system spans multiple transactions, with each transaction having its own persistence context:
649
647
650
-
Therefore, we may gain control over the way state is loaded before a `merge()` just by calling `find()` before calling `merge()`.
648
+
1. an entity `e` is retrieved in one persistence context, and then the current transaction ends, resulting in destruction of the persistence context and in the entity becoming detached, then
649
+
2. `e` is modified in some way while detached, and then
650
+
3. finally, the modification is made persistent by merging the detached instance in a second transaction, with a new persistence context, by calling `session.merge(e)`.
651
+
652
+
In step 3, the original entity instance `e` remains detached, but `merge()` returns a distinct instance `f` representing the same row of the database and associated with the new persistence context.
653
+
That is, `merge()` trades a detached instance for a <<persistence-contexts,persistent instance>> representing the same row.
654
+
655
+
To determine the nature of the modification held in `e` and to guarantee correct semantics with respect to optimistic locking, Hibernate first ``select``s the current persistent state of the entity from the database before applying the modification to `f`.
656
+
657
+
<<cascade,Cascade merge>> allows multiple modifications held in a graph of entity instances to be made persistent in one call to `merge()`.
658
+
When `merge()` is used with cascading -- that is, when the `merge()` operation is applied to a root entity with associations mapped `cascade=MERGE` -- Hibernate issues a single `select` statement to retrieve the current database state of the root entity and all its associated entities.
659
+
This behavior avoids the use of N+1 select statements for state retrieval during cascade merge but, in certain circumstances, the query might be suboptimal.
660
+
On the other hand, this query does not occur if the root entity was already loaded into the persistence context before `merge()` is called.
661
+
662
+
Therefore, when using the `EntityManager` API, we may gain control over the way state is loaded before a `merge()` just by calling `find()` before calling `merge()`.
651
663
652
664
[source,java]
653
665
----
654
666
Book book = ... ;
655
-
var graph =session.createEntityGraph(Book.class);
667
+
var graph =entityManager.createEntityGraph(Book.class);