Class Jpa
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classConfigures aJpa; instances are not thread-safe and are used during configuration. -
Method Summary
Modifier and TypeMethodDescriptionstatic Jpa.Builderbuilder()Starts a configuration that will use the supplied factory.<T> TentityManager(Context context) Returns the EntityManager bound to this request by this instance's middleware.Returns this instance's request-scoped EntityManager accessor.Returns middleware that gives each request its own EntityManager in read-only mode.Returns middleware that gives each request its own EntityManager and resource-local transaction.
-
Method Details
-
builder
Starts a configuration that will use the supplied factory.- Returns:
- a new builder
-
entityManagers
Returns this instance's request-scoped EntityManager accessor. DifferentJpainstances have different accessors, and an accessor only sees requests served by its own middleware.- Returns:
- a thread-safe accessor to pass to handlers
-
entityManager
Returns the EntityManager bound to this request by this instance's middleware.- Type Parameters:
T- type inferred by the caller, normallyjakarta.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
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 likereadOnlyTransaction().- Returns:
- new middleware; registration is what activates it
-
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
-