我正在用 Java 创建一个 Web 应用程序,并希望使用 Javalin 框架。
问题:
- 使用 JPA(通过 hibernate) noreferrer">Javalin Web 应用程序?
- 即,使 JPA EntityManager 对象可用于 Javalin 请求处理程序的最佳方法是什么?
更新:更多上下文
我想在我的 javalin 应用程序中使用 JPA(通过 Hibernate)。 JPA 的中心概念是使用 EntityManager 实例来访问数据库。要创建 EntityManager
,需要有一个 EntityManagerFactory
。
我当前所做的是创建一个全局 EntityManagerFactory
和使用数据库调用 factory.createEntityManager()
的处理程序。
虽然这有效,但我想知道是否还有其他推荐的方法?来自 Flask/SQLAlchemy/Python 背景 - 在该堆栈上,ORM 会话(EntityManager 等效项)通常可用作请求绑定对象。
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
EntityManager
object 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.
发布评论
评论(1)
我最终实现了以下内容:
Javalin
实例。然后调用enableRequestBoundEntityManager(javalinApp,entityManagerFactory)。IE:
I ended up implementing the following:
EntityManagerFactory
on startup. Create aJavalin
instance on startup. Then callenableRequestBoundEntityManager(javalinApp, entityManagerFactory)
.EntityManager
, use the extension propertyContext.entityManager
.I.e.: