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
+33-29Lines changed: 33 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -95,35 +95,6 @@ A great deal of human suffering has resulted from users mismanaging the lifecycl
95
95
We'll conclude by noting that whether a persistence context helps or harms the performance of a given unit of work depends greatly on the nature of the unit of work.
96
96
For this reason Hibernate provides both stateful and stateless sessions.
97
97
98
-
[[transient-vs-detached]]
99
-
=== Transient vs detached
100
-
101
-
Sometimes, Hibernate needs to be able to distinguish whether an entity instance is:
102
-
103
-
- a brand-new transient object the client just instantiated using `new`, or
104
-
- a detached object, which previously belonged to a persistence context.
105
-
106
-
This is a bit of a problem, since there's no good way for Hibernate to just tag an entity with a Post-it saying "I've seen you before" that wouldn't have a negative impact on performance.
107
-
108
-
Therefore, Hibernate uses heuristics.
109
-
The two most useful heuristics are:
110
-
111
-
1. If the entity has a <<generated-identifiers,generated identifier>>, the value of the id field is inspected: if the value currently assigned to the id field is the default value for the type of the field, then the object is transient; otherwise, the object is detached.
112
-
2. If the entity has a <<version-attributes,version>>, the value of the version field is inspected: if the value currently assigned to the version field is the default value, or a negative number, then the object is transient; otherwise, the object is detached.
113
-
114
-
If the entity has neither a generated id, nor a version, Hibernate usually falls back to just doing something reasonable.
115
-
In extreme cases a `SELECT` query will be issued to determine whether a matching row exists in the database.
116
-
117
-
[WARNING]
118
-
These heuristics aren't perfect.
119
-
It's quite easy to confuse Hibernate by assigning a value to the id field or version field, makeing a new transient instance look like it's detached.
120
-
We therefore strongly discourage assigning values to fields annotated `@GeneratedValue` or `@Version` before passing an entity to Hibernate.
121
-
122
-
[TIP]
123
-
If the heuristics ever happen cause a real problem, you may implement your own Post-it tagging via link:{doc-javadoc-url}org/hibernate/Interceptor.html#isTransient(java.lang.Object)[`Interceptor.isTransient()`].
124
-
The `Interceptor` interface -- along with its friend link:{doc-javadoc-url}org/hibernate/CustomEntityDirtinessStrategy.html[`CustomEntityDirtinessStrategy`] -- allows advanced users to augment the built-in handling of managed entities with custom behavior.
125
-
These interfaces are very useful if you're building your own persistence framework with Hibernate as the foundation.
126
-
127
98
[[creating-session]]
128
99
=== Creating a session
129
100
@@ -828,6 +799,39 @@ class OrderEvents {
828
799
----
829
800
A single entity listener class may even be a generic listener that receives lifecycle callbacks for multiple different entity classes.
830
801
802
+
[TIP]
803
+
The venerable link:{doc-javadoc-url}org/hibernate/Interceptor.html[`Interceptor`] interface is a more powerful alternative to entity listeners.
804
+
`Interceptor` and its friend link:{doc-javadoc-url}org/hibernate/CustomEntityDirtinessStrategy.html[`CustomEntityDirtinessStrategy`] allow advanced users to augment the built-in handling of managed entities with custom behavior.
805
+
These interfaces are very useful if you're building your own persistence framework with Hibernate as the foundation.
806
+
807
+
We're about to see one way `Interceptor` can be used.
808
+
809
+
[[transient-vs-detached]]
810
+
=== Transient vs detached
811
+
812
+
Sometimes, Hibernate needs to be able to distinguish whether an entity instance is:
813
+
814
+
- a brand-new transient object the client just instantiated using `new`, or
815
+
- a detached object, which previously belonged to a persistence context.
816
+
817
+
This is a bit of a problem, since there's no good and efficient way for Hibernate to just tag an entity with a Post-it saying "I've seen you before".
818
+
819
+
Therefore, Hibernate uses heuristics.
820
+
The two most useful heuristics are:
821
+
822
+
1. If the entity has a <<generated-identifiers,generated identifier>>, the value of the id field is inspected: if the value currently assigned to the id field is the default value for the type of the field, then the object is transient; otherwise, the object is detached.
823
+
2. If the entity has a <<version-attributes,version>>, the value of the version field is inspected: if the value currently assigned to the version field is the default value, or a negative number, then the object is transient; otherwise, the object is detached.
824
+
825
+
If the entity has neither a generated id, nor a version, Hibernate usually falls back to just doing something reasonable.
826
+
In extreme cases a `SELECT` query will be issued to determine whether a matching row exists in the database.
827
+
828
+
[WARNING]
829
+
These heuristics aren't perfect.
830
+
It's quite easy to confuse Hibernate by assigning a value to the id field or version field, making a new transient instance look like it's detached.
831
+
We therefore strongly discourage assigning values to fields annotated `@GeneratedValue` or `@Version` before passing an entity to Hibernate.
832
+
833
+
If the heuristics ever happen cause a real problem, you may implement your own Post-it tagging via link:{doc-javadoc-url}org/hibernate/Interceptor.html#isTransient(java.lang.Object)[`Interceptor.isTransient()`].
0 commit comments