I am creating a web application in Java and would like to use the Javalin framework.
Issues/questions:
- What would be the best way to use JPA (via hibernate) in a Javalin web application?
- I.e. what is the best way make a JPA
EntityManagerobject available to the Javalin request handlers?
Update: Some more context
I want to use JPA (via Hibernate) in my javalin application. The central concept in JPA is that you use an EntityManager instance to access the database. To create an EntityManager, there is a EntityManagerFactory.
What I currently do is that I create a global EntityManagerFactory and the handlers that use the database call factory.createEntityManager().
While this works, I wonder if there are other recommended apporaches? Coming from a Flask/SQLAlchemy/Python background - on that stack the ORM session (EntityManager equivalent) is typically available as a request-bound object.
I ended up implementing the following:
EntityManagerFactoryon startup. Create aJavalininstance on startup. Then callenableRequestBoundEntityManager(javalinApp, entityManagerFactory).EntityManager, use the extension propertyContext.entityManager.I.e.: