Skip to content

Commit d57e09c

Browse files
committed
add javadoc to Session regarding proxy objects
1 parent 97099c0 commit d57e09c

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

hibernate-core/src/main/java/org/hibernate/Session.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,16 @@
4444
* <p>
4545
* At any given time, an instance may be associated with at most one open session.
4646
* <p>
47-
* Any instance returned by {@link #get(Class, Object)} or by a query is persistent.
47+
* Any instance returned by {@link #get(Class, Object)}, {@link #find(Class, Object)},
48+
* or by a query is persistent. A persistent instance might hold references to other
49+
* entity instances, and sometimes these references are <em>proxied</em> by an
50+
* intermediate object. When an associated entity has not yet been fetched from the
51+
* database, references to the unfetched entity are represented by uninitialized
52+
* proxies. The state of an unfetched entity is automatically fetched from the
53+
* database when a method of its proxy is invoked, if and only if the proxy is
54+
* associated with an open session. Otherwise, {@link #getReference(Object)} may be
55+
* used to trade a proxy belonging to a closed session for a new proxy associated
56+
* with the current session.
4857
* <p>
4958
* A transient instance may be made persistent by calling {@link #persist(Object)}.
5059
* A persistent instance may be made detached by calling {@link #detach(Object)}.
@@ -969,6 +978,9 @@ public interface Session extends SharedSessionContract, EntityManager {
969978
* <p>
970979
* This operation is very similar to {@link #find(Class, Object)}.
971980
* <p>
981+
* The object returned by {@code get()} or {@code find() } is either an unproxied instance
982+
* of the given entity class, of a fully-fetched proxy object.
983+
* <p>
972984
* This operation requests {@link LockMode#NONE}, that is, no lock, allowing the object
973985
* to be retrieved from the cache without the cost of database access. However, if it is
974986
* necessary to read the state from the database, the object will be returned with the
@@ -1086,6 +1098,15 @@ public interface Session extends SharedSessionContract, EntityManager {
10861098
* <p>
10871099
* Note that {@link Hibernate#createDetachedProxy(SessionFactory, Class, Object)}
10881100
* may be used to obtain a <em>detached</em> reference.
1101+
* <p>
1102+
* It's sometimes necessary to narrow a reference returned by {@code getReference()}
1103+
* to a subtype of the given entity type. A direct Java typecast should never be used
1104+
* in this situation. Instead, the method {@link Hibernate#unproxy(Object, Class)} is
1105+
* the recommended way to narrow the type of a proxy object. Alternatively, a new
1106+
* reference may be obtained by simply calling {@code getReference()} again, passing
1107+
* the subtype. Either way, the narrowed reference will usually not be identical to
1108+
* the original reference, when the references are compared using the {@code ==}
1109+
* operator.
10891110
*
10901111
* @param entityType the entity type
10911112
* @param id the identifier of a persistent instance that exists in the database

0 commit comments

Comments
 (0)