Operations like isInitialized(Object) and initialize(Object) are
of general purpose. But createDetachedProxy(SessionFactory, Class, Object)
and CollectionInterface#createDetachedInstance() are intended for use by
generic code that must materialize an "amputated" graph of Hibernate entities.
(For example, a library which deserializes entities from JSON.)
Lazy fetching of a one to one or many to one association requires special bytecode tricks. The tricks used depend on whether build-time bytecode enhancement is enabled.
When bytecode enhancement is not used, an unfetched lazy association is represented by a proxy object which holds the identifier (foreign key) of the associated entity instance.
getClass(Object) must be used in place of Object#getClass(),
and this method fetches the entity by side-effect.
unproxy(Object, Class)} must be used to perform typecasts, and
getClass(Object) must be used instead of the Java instanceof
operator.
instanceof operator, and Object#getClass()
may be used as normal.
Graphs of Hibernate entities obtained from a Session are usually in an
amputated form, with associations and collections replaced by proxies and lazy
collections. (That is, by instances of the internal types HibernateProxy
and PersistentCollection.) These objects are fully serializable using
Java serialization, but can cause discomfort when working with custom serialization
libraries. Therefore, this class defines operations that may be used to write code
that completely removes the amputated leaves of the graph (the proxies) during
serialization, and rematerializes and reattaches them during deserialization. It's
possible, in principle, to use these operations, together with reflection, or with
the Hibernate metamodel, to write such generic code for any given serialization
library, but the details depend on what facilities the library itself offers for
the program to intervene in the process of serialization and of deserialization.
| Modifier and Type | Class and Description |
|---|---|
| public static class | Hibernate.CollectionInterface<
the type of collection, for example, C>List<User>Operations for obtaining references to persistent collections of a certain type. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static < the element type U> Hibernate. | bag()
Obtain an instance of |
| public static <C> Hibernate. | collection(Class<C>
the Java class object representing the collection type collectionClass)Obtain an instance of |
| public static <T> boolean | Returns: true if the collection does contain the given elementa persistent collection associated with an open session collection, T element)Determine if the given persistent collection contains the given element, without fetching its state from the database. |
| public static <E> E | Returns: a detached uninitialized proxythe session factory with which the entity is associated sessionFactory, Class<E> the entity class entityClass, Object the id of the persistent entity instance id)Obtain a detached, uninitialized reference (a proxy) for a persistent entity with the given identifier. |
| public static <K, V> V | |
| public static <T> T | |
| public static <T> Class | Returns: the true class of the instancean entity instance or proxy proxy)Get the true, underlying class of a proxied entity. |
| public static <T> Class | Returns: the true class of the instancean entity instance or proxy proxy)Get the true, underlying class of a proxied entity. |
| public static void | initialize(Object
a persistable object, proxy, persistent collection or proxy)nullForce initialization of a proxy or persistent collection. |
| public static boolean | Returns: true if the argument is already initialized, or is not a proxy or collectiona persistable object, proxy, persistent collection or proxy)nullDetermines if the given proxy or persistent collection is initialized. |
| public static boolean | Returns: true if the entity is an instance of the given classan entity instance or proxy proxy, Class<?> entityClass)Determine if the true, underlying class of the proxied entity is assignable to the given class. |
| public <E> boolean | Returns: true if the named property of the object is not listed as uninitialized; false otherwiseThe entity instance or proxy entity, Attribute<? super E, ?> A persistent attribute of the entity attribute)Determines if the given attribute of the given entity instance is initialized. |
| public static boolean | Returns: true if the named property of the object is not listed as uninitialized; false otherwiseThe entity instance or proxy proxy, String the name of a persistent attribute of the object attributeName)Determines if the property with the given name of the given entity instance is initialized. |
| public static < the element type U> Hibernate. | list()
Obtain an instance of |
| public static < the key type U, the value type V> Hibernate. | map()
Obtain an instance of |
| public static < the element type U> Hibernate. | set()
Obtain an instance of |
| public static int | Returns: the size of the collectiona persistent collection associated with an open session collection)Obtain the size of a persistent collection, without fetching its state from the database. |
| public static < the key type U, the value type V> Hibernate. | sortedMap()
Obtain an instance of |
| public static < the element type U> Hibernate. | sortedSet()
Obtain an instance of |
| public static Object | |
| public static <T> T |
| Hibernate | back to summary |
|---|---|
| private Hibernate() Cannot be instantiated. | |
| bag | back to summary |
|---|---|
| public static <U> Hibernate. Obtain an instance of
| |
| collection | back to summary |
|---|---|
| public static <C> Hibernate. Obtain an instance of
| |
| contains | back to summary |
|---|---|
| public static <T> boolean contains(Collection<? super T> collection, T element) Determine if the given persistent collection contains the given element, without fetching its state from the database.
| |
| createDetachedProxy | back to summary |
|---|---|
| public static <E> E createDetachedProxy(SessionFactory sessionFactory, Class<E> entityClass, Object id) Obtain a detached, uninitialized reference (a proxy) for a persistent entity with the given identifier.
The returned proxy is not associated with any session, and cannot be initialized
by calling
| |
| get | back to summary |
|---|---|
| public static <K, V> V get(Map<? super K, V> map, K key) Obtain the value associated with the given key by the given persistent map, without fetching the state of the map from the database.
| |
| get | back to summary |
|---|---|
| public static <T> T get(List<T> list, int key) Obtain the element of the given persistent list with the given index, without fetching the state of the list from the database.
| |
| getClass | back to summary |
|---|---|
| public static <T> Class Get the true, underlying class of a proxied entity. This operation will initialize a proxy by side effect.
| |
| getClassLazy | back to summary |
|---|---|
| public static <T> Class Get the true, underlying class of a proxied entity.
Like
| |
| initialize | back to summary |
|---|---|
| public static void initialize(Object proxy) throws HibernateException Force initialization of a proxy or persistent collection. In the case of a many-valued association, only the collection itself is initialized. It is not guaranteed that the associated entities held within the collection will be initialized.
| |
| isInitialized | back to summary |
|---|---|
| public static boolean isInitialized(Object proxy) Determines if the given proxy or persistent collection is initialized.
This operation is equivalent to
| |
| isInstance | back to summary |
|---|---|
| public static boolean isInstance(Object proxy, Class<?> entityClass) Determine if the true, underlying class of the proxied entity is assignable to the given class. This operation will initialize a proxy by side effect.
| |
| isPropertyInitialized | back to summary |
|---|---|
| public <E> boolean isPropertyInitialized(E entity, Attribute<? super E, ?> attribute) Determines if the given attribute of the given entity instance is initialized.
| |
| isPropertyInitialized | back to summary |
|---|---|
| public static boolean isPropertyInitialized(Object proxy, String attributeName) Determines if the property with the given name of the given entity instance is
initialized. If the named property does not exist or is not persistent, this
method always returns
This operation is equivalent to | |
| list | back to summary |
|---|---|
| public static <U> Hibernate. Obtain an instance of
| |
| map | back to summary |
|---|---|
| public static <U, V> Hibernate. Obtain an instance of
| |
| set | back to summary |
|---|---|
| public static <U> Hibernate. Obtain an instance of
| |
| size | back to summary |
|---|---|
| public static int size(Collection<?> collection) Obtain the size of a persistent collection, without fetching its state from the database.
| |
| sortedMap | back to summary |
|---|---|
| public static <U, V> Hibernate. Obtain an instance of
| |
| sortedSet | back to summary |
|---|---|
| public static <U> Hibernate. Obtain an instance of
| |
| unproxy | back to summary |
|---|---|
| public static Object unproxy(Object proxy) If the given object is not a proxy, return it. But, if it is a proxy, ensure that the proxy is initialized, and return a direct reference to its proxied entity object.
| |
| unproxy | back to summary |
|---|---|
| public static <T> T unproxy(T proxy, Class<T> entityClass) If the given object is not a proxy, cast it to the given type, and return it. But, if it is a proxy, ensure that the proxy is initialized, and return a direct reference to its proxied entity object, after casting to the given type.
| |
List<User>
| Modifier and Type | Field and Description |
|---|---|
| private final Supplier | |
| private final Supplier |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public C | Returns: an uninitialized persistent collectionObtain a detached, uninitialized persistent collection of the type represented by this object. |
| public C | Returns: a newly-instantiated empty collectionInstantiate an empty collection of the type represented by this object. |
| created | back to summary |
|---|---|
| private final Supplier<C> created | |
| detached | back to summary |
|---|---|
| private final Supplier<C> detached | |
| CollectionInterface | back to summary |
|---|---|
| private CollectionInterface(Supplier<C> detached, Supplier<C> created) | |
| createDetachedInstance | back to summary |
|---|---|
| public C createDetachedInstance() Obtain a detached, uninitialized persistent collection of the type represented by this object.
The returned wrapper object is not associated with any session, and cannot be
initialized by calling
| |
| createNewInstance | back to summary |
|---|---|
| public C createNewInstance() Instantiate an empty collection of the type represented by this object.
| |