Class SessionRequest

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

public class SessionRequest extends Object implements io.jooby.Route.Filter
Attach Session and javax.persistence.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 Summary

    Constructors
    Constructor
    Description
    Creates a new session request and attach to the default/first session factory registered.
    Creates a new session request and attach the to a named session factory.
  • Method Summary

    Modifier and Type
    Method
    Description
    io.jooby.Route.Handler
    apply(io.jooby.Route.Handler next)
     
    io.jooby.ServiceKey<org.hibernate.SessionFactory>
    Get the service key for accessing to the configured SessionFactory service.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface io.jooby.Route.Aware

    setRoute

    Methods inherited from interface io.jooby.Route.Filter

    then, then
  • 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 io.jooby.Route.Handler apply(@NonNull io.jooby.Route.Handler next)
      Specified by:
      apply in interface io.jooby.Route.Filter
    • getSessionFactoryKey

      @NonNull public io.jooby.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.