Skip to content

Commit 59d3453

Browse files
committed
much better placement/flow of new doc section
1 parent 72fa92b commit 59d3453

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

documentation/src/main/asciidoc/introduction/Interacting.adoc

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -95,35 +95,6 @@ A great deal of human suffering has resulted from users mismanaging the lifecycl
9595
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.
9696
For this reason Hibernate provides both stateful and stateless sessions.
9797

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-
12798
[[creating-session]]
12899
=== Creating a session
129100

@@ -828,6 +799,39 @@ class OrderEvents {
828799
----
829800
A single entity listener class may even be a generic listener that receives lifecycle callbacks for multiple different entity classes.
830801

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()`].
834+
831835

832836
[[jdbc]]
833837
=== Interacting directly with JDBC

0 commit comments

Comments
 (0)