Class EntityManagers

java.lang.Object
com.jsgalactic.axiom.jpa.EntityManagers

public final class EntityManagers extends Object
Request-scoped accessor for the EntityManager bound by Jpa.transaction().

An instance is obtained from a Jpa instance with Jpa.entityManagers() and may be shared by every handler of an application. It holds no thread-local state: the key is the request's own Context, which Axiom creates once per request and passes to middleware, handlers and error handlers alike. The entry is removed when the middleware finishes, so an EntityManager cannot leak into a later request on the same thread or be seen after the response.

The return type is inferred from the assignment so that no JPA type appears in an Axiom signature:


 EntityManager entityManager = managers.get(context);
 

Instances are thread-safe; a caller must use the returned EntityManager only on the request thread and only until its handler returns, as required by JPA.

  • Method Details

    • get

      public <T> T get(Context context)
      Returns the EntityManager bound to this request.
      Type Parameters:
      T - the type inferred by the caller, normally jakarta.persistence.EntityManager
      Parameters:
      context - the request being served
      Returns:
      the request's EntityManager, cast to the inferred type
      Throws:
      IllegalStateException - if no JPA transaction middleware is active for this request
      ClassCastException - if the caller asks for a type the EntityManager does not have
    • has

      public boolean has(Context context)
      Reports whether an EntityManager is bound to this request.
      Parameters:
      context - the request being served
      Returns:
      true while JPA transaction middleware is active for this request