在过去的三天里,我通过各种示例学习 JPA,将我的 JDBC 代码更改为 JPA。每个 JPA 示例和教程都有运行它的 main 方法。当然, 在 main 方法中仅定义 EntityManager 和 EntityManagerFactory。
我的 Web 应用程序的服务器端代码中没有 main 方法。这是使用 JPA 的问题吗?或者在其他类中创建 EntityManager 和 EntityManagerFactory 被允许。 如果有人向我解释一下如何在 JPA 中使用 hibernate,我将不胜感激。
最佳答案
Every JPA example and tutorial have main method to run it.
这仅对打算在容器外部运行的代码有用,这通常是教程的情况。
I don't have main method in my web application's server side code. Is that a problem in point of using JPA.
没有。绝对不是。
Or Creation of EntityManager & EntityManagerFactory in other classes is allowed.
对 EntityManagerFactory
和 EntityManager
没有特殊限制(除此之外,EMF应该在应用程序的生命周期内创建一次Web 应用程序中 EM 的常见模式是 entityManager-per-request,即在请求开始时打开 EntityManager
并在结束时刷新并关闭它)。
And it would be appreciative, if anyone explain me about using Hibernate in JPA.
Hibernate 可以用作 JPA 提供程序(更准确地说,实现是由卫星项目 Hibernate EntityManager 提供的)。但你的问题太模糊了。如果您遇到特定问题,请提出另一个问题并进行描述。
关于java - 我应该包含 JPA 的 main 方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3741839/