Class SessionRequest

java.lang.Object
io.jooby.hibernate.SessionRequest
All Implemented Interfaces:
Route.Aware, Route.Filter

public class SessionRequest extends Object implements Route.Filter
Attach Session and EntityManager to the current request.

The active Session is accessible via SessionFactory.getCurrentSession() for the duration of the route pipeline.

Once route pipeline is executed the session/entityManager is detached from current request and closed.

Usage:


 {

   install(new HikariModule());

   install(new HibernateModule());

   use(new SessionRequest());

   get("/handle", ctx -> {
     EntityManager handle = require(EntityManager.class);
     // work with handle.
   });
 }
 
NOTE: This is NOT the open session in view pattern. Persistent objects must be fully initialized to be encoded/rendered to the client. Otherwise, Hibernate results in LazyInitializationException.
Author:
Benjamin Quinn
  • Constructor Details

    • SessionRequest

      public SessionRequest(@NonNull String name)
      Creates a new session request and attach the to a named session factory.
      Parameters:
      name - Name of the session factory.
    • SessionRequest

      public SessionRequest()
      Creates a new session request and attach to the default/first session factory registered.
  • Method Details

    • apply

      @NonNull public Route.Handler apply(@NonNull Route.Handler next)
      Specified by:
      apply in interface Route.Filter
    • getSessionFactoryKey

      @NonNull public ServiceKey<org.hibernate.SessionFactory> getSessionFactoryKey()
      Get the service key for accessing to the configured SessionFactory service.
      Returns:
      The service key for accessing to the configured SessionFactory service.