Package org.hibernate

Interface StatelessSession

  • All Superinterfaces:
    AutoCloseable, Closeable, QueryProducer, Serializable, SharedSessionContract
    All Known Implementing Classes:
    StatelessSessionImpl

    public interface StatelessSession
    extends SharedSessionContract
    A command-oriented API often used for performing bulk operations against the database. A stateless session has no persistence context, and always works directly with detached entity instances. When a method of this interface is called, any necessary interaction with the database happens immediately and synchronously.

    Viewed in opposition to Session, the StatelessSession is a whole competing programming model, one preferred by some developers for its simplicity and somewhat lower level of abstraction. But the two kinds of session are not enemies, and may comfortably coexist in a single program.

    A stateless session comes some with designed-in limitations:

    • it does not have a first-level cache,
    • nor interact with any second-level cache,
    • nor does it implement transactional write-behind or automatic dirty checking, and
    • nor do operations cascade to associated instances.

    Furthermore:

    • collections are completely ignored by a stateless session, and
    • operations performed via a stateless session bypass Hibernate's event model, lifecycle callbacks, and interceptors.

    Stateless sessions are vulnerable to data aliasing effects, due to the lack of a first-level cache.

    On the other hand, for certain kinds of transactions, a stateless session may perform slightly faster than a stateful session.

    • Method Detail

      • insert

        Object insert​(Object entity)
        Insert a row.
        Parameters:
        entity - a new transient instance
        Returns:
        The identifier of the inserted entity
      • insert

        Object insert​(String entityName,
                      Object entity)
        Insert a row.
        Parameters:
        entityName - The entityName for the entity to be inserted
        entity - a new transient instance
        Returns:
        the identifier of the instance
      • update

        void update​(Object entity)
        Update a row.
        Parameters:
        entity - a detached entity instance
      • update

        void update​(String entityName,
                    Object entity)
        Update a row.
        Parameters:
        entityName - The entityName for the entity to be updated
        entity - a detached entity instance
      • delete

        void delete​(Object entity)
        Delete a row.
        Parameters:
        entity - a detached entity instance
      • delete

        void delete​(String entityName,
                    Object entity)
        Delete a row.
        Parameters:
        entityName - The entityName for the entity to be deleted
        entity - a detached entity instance
      • upsert

        @Incubating
        void upsert​(Object entity)
        Use a SQL merge into statement to perform an upsert.
        Parameters:
        entity - a detached entity instance
        Throws:
        TransientObjectException - is the entity is transient
        Since:
        6.3
      • upsert

        @Incubating
        void upsert​(String entityName,
                    Object entity)
        Use a SQL merge into statement to perform an upsert.
        Parameters:
        entityName - The entityName for the entity to be merged
        entity - a detached entity instance
        Throws:
        TransientObjectException - is the entity is transient
        Since:
        6.3
      • get

        Object get​(String entityName,
                   Object id)
        Retrieve a row.
        Parameters:
        entityName - The name of the entity to retrieve
        id - The id of the entity to retrieve
        Returns:
        a detached entity instance
      • get

        <T> T get​(Class<T> entityClass,
                  Object id)
        Retrieve a row.
        Parameters:
        entityClass - The class of the entity to retrieve
        id - The id of the entity to retrieve
        Returns:
        a detached entity instance
      • get

        Object get​(String entityName,
                   Object id,
                   LockMode lockMode)
        Retrieve a row, obtaining the specified lock mode.
        Parameters:
        entityName - The name of the entity to retrieve
        id - The id of the entity to retrieve
        lockMode - The lock mode to apply to the entity
        Returns:
        a detached entity instance
      • get

        <T> T get​(Class<T> entityClass,
                  Object id,
                  LockMode lockMode)
        Retrieve a row, obtaining the specified lock mode.
        Parameters:
        entityClass - The class of the entity to retrieve
        id - The id of the entity to retrieve
        lockMode - The lock mode to apply to the entity
        Returns:
        a detached entity instance
      • get

        <T> T get​(EntityGraph<T> graph,
                  GraphSemantic graphSemantic,
                  Object id)
        Retrieve a row, fetching associations specified by the given EntityGraph.
        Parameters:
        graph - The EntityGraph
        graphSemantic - a GraphSemantic specifying how the graph should be interpreted
        id - The id of the entity to retrieve
        Returns:
        a detached entity instance
        Since:
        6.3
      • get

        <T> T get​(EntityGraph<T> graph,
                  GraphSemantic graphSemantic,
                  Object id,
                  LockMode lockMode)
        Retrieve a row, fetching associations specified by the given EntityGraph, and obtaining the specified lock mode.
        Parameters:
        graph - The EntityGraph
        graphSemantic - a GraphSemantic specifying how the graph should be interpreted
        id - The id of the entity to retrieve
        lockMode - The lock mode to apply to the entity
        Returns:
        a detached entity instance
        Since:
        6.3
      • refresh

        void refresh​(Object entity)
        Refresh the entity instance state from the database.
        Parameters:
        entity - The entity to be refreshed.
      • refresh

        void refresh​(String entityName,
                     Object entity)
        Refresh the entity instance state from the database.
        Parameters:
        entityName - The entityName for the entity to be refreshed.
        entity - The entity to be refreshed.
      • refresh

        void refresh​(Object entity,
                     LockMode lockMode)
        Refresh the entity instance state from the database.
        Parameters:
        entity - The entity to be refreshed.
        lockMode - The LockMode to be applied.
      • refresh

        void refresh​(String entityName,
                     Object entity,
                     LockMode lockMode)
        Refresh the entity instance state from the database.
        Parameters:
        entityName - The entityName for the entity to be refreshed.
        entity - The entity to be refreshed.
        lockMode - The LockMode to be applied.
      • fetch

        void fetch​(Object association)
        Fetch an association that's configured for lazy loading.

        Warning: this operation in a stateless session is quite sensitive to data aliasing effects and should be used with great care.

        Parameters:
        association - a lazy-loaded association
        Since:
        6.0
        See Also:
        Hibernate.initialize(Object)