Package org.hibernate

Class Hibernate


  • public final class Hibernate
    extends Object
    Various utility functions for working with proxies and lazy collection references.

    Operations like isInitialized(Object) and initialize(Object) are of general purpose. But createDetachedProxy(SessionFactory, Class, Object) and createDetachedProxy(SessionFactory, Class, Object) 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.)

    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.

    • Method Detail

      • initialize

        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.
        Parameters:
        proxy - a persistable object, proxy, persistent collection or null
        Throws:
        HibernateException - if the proxy cannot be initialized at this time, for example, if the Session was closed
      • isInitialized

        public static boolean isInitialized​(Object proxy)
        Check if the proxy or persistent collection is initialized.
        Parameters:
        proxy - a persistable object, proxy, persistent collection or null
        Returns:
        true if the argument is already initialized, or is not a proxy or collection
      • getClass

        public static <T> Class<? extends T> getClass​(T proxy)
        Get the true, underlying class of a proxied persistent class. This operation will initialize a proxy by side effect.
        Parameters:
        proxy - an entity instance or proxy
        Returns:
        the true class of the instance
      • isPropertyInitialized

        public static boolean isPropertyInitialized​(Object proxy,
                                                    String propertyName)
        Check if the property is initialized. If the named property does not exist or is not persistent, this method always returns true.
        Parameters:
        proxy - The potential proxy
        propertyName - the name of a persistent attribute of the object
        Returns:
        true if the named property of the object is not listed as uninitialized; false otherwise
      • unproxy

        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.
        Parameters:
        proxy - an object which might be a proxy for an entity
        Returns:
        a reference that is never proxied
        Throws:
        LazyInitializationException - if this operation is called on an uninitialized proxy that is not associated with an open session.
      • unproxy

        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.
        Parameters:
        proxy - an object which might be a proxy for an entity
        entityClass - an entity type to cast to
        Returns:
        a reference that is never proxied
        Throws:
        LazyInitializationException - if this operation is called on an uninitialized proxy that is not associated with an open session.
      • createDetachedProxy

        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 initialize(Object). It can be used to represent a reference to the entity when working with a detached object graph.
        Parameters:
        sessionFactory - the session factory with which the entity is associated
        entityClass - the entity class
        id - the id of the persistent entity instance
        Returns:
        a detached uninitialized proxy