Interface Stage.StatelessSession

  • All Superinterfaces:
    Stage.Closeable
    Enclosing interface:
    Stage

    public static interface Stage.StatelessSession
    extends Stage.Closeable
    A non-blocking counterpart to the Hibernate StatelessSession interface, which provides a command-oriented API for performing bulk operations against a database.

    A stateless session does not implement a first-level cache nor interact with any second-level cache, nor does it implement transactional write-behind or automatic dirty checking, nor do operations cascade to associated instances. Changes to many to many associations and element collections may not be made persistent in a stateless session. Operations performed via a stateless session bypass Hibernate's event model and interceptors.

    For certain kinds of work, a stateless session may perform slightly better than a stateful session.

    In particular, for a session which loads many entities, use of a StatelessSession alleviates the need to call:

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

    See Also:
    StatelessSession
    • Method Detail

      • get

        <T> CompletionStage<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, via a CompletionStage
        See Also:
        StatelessSession.get(Class, Object, LockMode)
      • get

        default <T> CompletionStage<T> get​(Class<T> entityClass,
                                           Object id,
                                           jakarta.persistence.LockModeType lockModeType)
        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
        lockModeType - The lock mode to apply to the entity
        Returns:
        a detached entity instance, via a CompletionStage
        See Also:
        StatelessSession.get(Class, Object, LockMode)
      • get

        <T> CompletionStage<T> get​(jakarta.persistence.EntityGraph<T> entityGraph,
                                   Object id)
        Retrieve a row, using the given EntityGraph as a fetch plan.
        Parameters:
        entityGraph - an EntityGraph specifying the entity and associations to be fetched
        id - The id of the entity to retrieve
        Returns:
        a detached entity instance, via a CompletionStage
      • createNamedQuery

        <R> Stage.Query<R> createNamedQuery​(String queryName)
        Create an instance of Stage.Query for the named query.
        Parameters:
        queryName - The name of the query
        Returns:
        The Stage.Query instance for manipulation and execution
        See Also:
        EntityManager.createQuery(String)
      • createNamedQuery

        <R> Stage.SelectionQuery<R> createNamedQuery​(String queryName,
                                                     Class<R> resultType)
        Create an instance of Stage.Query for the named query.
        Parameters:
        queryName - The name of the query
        resultType - the Java type returned in each row of query results
        Returns:
        The Stage.Query instance for manipulation and execution
        See Also:
        EntityManager.createQuery(String, Class)
      • createQuery

        <R> Stage.SelectionQuery<R> createQuery​(jakarta.persistence.criteria.CriteriaQuery<R> criteriaQuery)
        Create an instance of Stage.SelectionQuery for the given criteria query.
        Parameters:
        criteriaQuery - The CriteriaQuery
        Returns:
        The Stage.SelectionQuery instance for manipulation and execution
        See Also:
        EntityManager.createQuery(String)
      • createQuery

        <R> Stage.MutationQuery createQuery​(jakarta.persistence.criteria.CriteriaUpdate<R> criteriaUpdate)
        Create an instance of Stage.MutationQuery for the given criteria update.
        Parameters:
        criteriaUpdate - The CriteriaUpdate
        Returns:
        The Stage.MutationQuery instance for manipulation and execution
        See Also:
        EntityManager.createQuery(String)
      • createQuery

        <R> Stage.MutationQuery createQuery​(jakarta.persistence.criteria.CriteriaDelete<R> criteriaDelete)
        Create an instance of Stage.MutationQuery for the given criteria delete.
        Parameters:
        criteriaDelete - The CriteriaDelete
        Returns:
        The Stage.MutationQuery instance for manipulation and execution
        See Also:
        EntityManager.createQuery(String)
      • fetch

        <T> CompletionStage<T> fetch​(T association)
        Asynchronously fetch an association that's configured for lazy loading.
         session.fetch(author.getBook()).thenAccept(book -> print(book.getTitle()))
         
        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
        Returns:
        the fetched association, via a CompletionStage
        See Also:
        Hibernate.initialize(Object)
      • getResultSetMapping

        <T> ResultSetMapping<T> getResultSetMapping​(Class<T> resultType,
                                                    String mappingName)
        Obtain a native SQL result set mapping defined via the annotation SqlResultSetMapping.
      • getEntityGraph

        <T> jakarta.persistence.EntityGraph<T> getEntityGraph​(Class<T> rootType,
                                                              String graphName)
        Obtain a named EntityGraph
      • createEntityGraph

        <T> jakarta.persistence.EntityGraph<T> createEntityGraph​(Class<T> rootType)
        Create a new mutable EntityGraph
      • createEntityGraph

        <T> jakarta.persistence.EntityGraph<T> createEntityGraph​(Class<T> rootType,
                                                                 String graphName)
        Create a new mutable copy of a named EntityGraph
      • withTransaction

        <T> CompletionStage<T> withTransaction​(Function<Stage.Transaction,​CompletionStage<T>> work)
        Performs the given work within the scope of a database transaction, automatically flushing the session. The transaction will be rolled back if the work completes with an uncaught exception, or if Stage.Transaction.markForRollback() is called.
      • If there is already a transaction associated with this session, the work is executed in the context of the existing transaction, and no new transaction is initiated.
      • If there is no transaction associated with this session, a new transaction is started, and the work is executed in the context of the new transaction.
Parameters:
work - a function which accepts Stage.Transaction and returns the result of the work as a CompletionStage.
See Also:
Stage.SessionFactory.withTransaction(BiFunction)