Class Jpa

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

public final class Jpa extends Object
Per-request JPA EntityManager and transaction boundaries for Axiom applications.

 var jpa = Jpa.builder().entityManagerFactory(applicationOwnedFactory).build();
 var managers = jpa.entityManagers();
 app.use(jpa.transaction());
 app.get("/items", ctx -> managers.get(ctx));
 

The application owns the EntityManagerFactory and closes it. The value is accepted as Object so no JPA type appears in an Axiom signature; Jpa.Builder.build() rejects a value that is not a factory with IllegalArgumentException. Nothing is opened, registered or changed until transaction() or readOnlyTransaction() is registered as middleware. The middleware opens one EntityManager for each request, binds it to that request's Context, runs the rest of the chain, and commits or rolls back before closing it. A request that already carries an active JTA transaction, reported by the callback supplied to Jpa.Builder.jta(JtaTransactionLookup), joins that transaction instead of beginning a local one.

Instances are immutable and thread-safe. The factory must be thread-safe, as required by JPA.

  • Method Details

    • builder

      public static Jpa.Builder builder()
      Starts a configuration that will use the supplied factory.
      Returns:
      a new builder
    • entityManagers

      public EntityManagers entityManagers()
      Returns this instance's request-scoped EntityManager accessor. Different Jpa instances have different accessors, and an accessor only sees requests served by its own middleware.
      Returns:
      a thread-safe accessor to pass to handlers
    • entityManager

      public <T> T entityManager(Context context)
      Returns the EntityManager bound to this request by this instance's middleware.
      Type Parameters:
      T - type inferred by the caller, normally jakarta.persistence.EntityManager
      Parameters:
      context - the request being served
      Returns:
      the request's EntityManager
      Throws:
      IllegalStateException - if the transaction middleware is not active for this request
    • transaction

      public Middleware transaction()
      Returns middleware that gives each request its own EntityManager and resource-local transaction. A response below 400 commits; an exception, a response of 400 or above, or a problem response rolls back. The EntityManager is closed on every path. A configured read-only setting makes this middleware behave like readOnlyTransaction().
      Returns:
      new middleware; registration is what activates it
    • readOnlyTransaction

      public Middleware readOnlyTransaction()
      Returns middleware that gives each request its own EntityManager in read-only mode. Flushing is deferred and the resource-local transaction is rolled back when the chain returns, so no write survives the request; an exception rolls it back as well. A joined JTA transaction is left to its owner.
      Returns:
      new middleware; registration is what activates it