为什么 JPA 注入在 @PersistentUnit 上不起作用

发布于 2024-08-27 10:34:39 字数 1027 浏览 5 评论 0原文

这是问题的继续( struts 2 bean 未创建 ) 我在 Tomcat 下的非常简单的 Web 应用程序中使用 struts2 + toplink。 在页面上我想使用迭代标签。这就是为什么我声明了一些工厂(SomeFactory)来解析实体(Entity)的集合。 每篇文章: http://download -uk.oracle.com/docs/cd/B32110_01/web.1013/b28221/usclient005.htm#CIHCEHHG 我唯一需要的是声明:

@PersistenceContext(unitName="name_in_persistence_xml")
public class SomeFactory
{
    @PersistenceUnit(unitName="name_in_persistence_xml")
    EntityManagerFactory emf;

    public EntityManager getEntityManager() {
       assert(emf != null); //HERE every time it is null
       return emf.createEntityManager();
    }
    public Collection<Entity> getAll()
    {
       return getEntityManager().createNamedQuery("Entity.findAll").getResultList();
}
}

出了什么问题?我可能错过了 web.xml 中的某些内容吗?如何为 Web 应用程序预初始化 toplink 以允许注入发生?

It is continues of question ( struts 2 bean is not created )
I'm using struts2 + toplink in my very simple web application under Tomcat.
On the page I would like use iteration tag. That is why I've declared some factory (SomeFactory) that resolves collection of entities (Entity).
Per article: http://download-uk.oracle.com/docs/cd/B32110_01/web.1013/b28221/usclient005.htm#CIHCEHHG
the only thing I need is declaration:

@PersistenceContext(unitName="name_in_persistence_xml")
public class SomeFactory
{
    @PersistenceUnit(unitName="name_in_persistence_xml")
    EntityManagerFactory emf;

    public EntityManager getEntityManager() {
       assert(emf != null); //HERE every time it is null
       return emf.createEntityManager();
    }
    public Collection<Entity> getAll()
    {
       return getEntityManager().createNamedQuery("Entity.findAll").getResultList();
}
}

What is wrong? May be i miss something in web.xml? How to pre-init toplink for web application to allow injection happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮光沉寂 2024-09-03 10:34:39

Tomcat 不会注入任何非 Java EE 容器的内容(即使使用 Java EE 5 容器,注入也仅适用于托管组件,如 servlet、过滤器、侦听器、EJB、Web 服务端点...)。因此,您必须手动创建 EntityManagerFactory(通常在 servlet 或帮助程序类中)并从中获取 EntityManager

EntityManagerFactory emf  = Persistence.createEntityManagerFactory(PU_NAME);
EntityManager entityManager = emf.createEntityManager();

请注意,创建 EntityManagerFactory code> 是一项成本高昂的操作,不应为每个请求都执行此操作。但是,创建 EntityManager 则不然,您应该为每个线程创建一个。但就您而言,我建议使用 struts2-persistenceplugin 来处理此问题。

谢谢,但似乎 [...] Java EE 并不强制要求使用注入 [...] Spring 为其提供了必要的引擎。

的确。但是您在其他问题中写了“根本没有弹簧”,并且您没有列出任何可以提供开箱即用注射的部件。无论如何,请查看 struts2-persistenceplugin,它可能足以满足您的需求。

You won't get anything injected by Tomcat which is not a Java EE container (and even with a Java EE 5 container, injection only works for managed components like servlets, filters, listeners, EJB, web service endpoints...). So you will have to create the EntityManagerFactory manually (typically in a servlet or a helper class) and get the EntityManager from it:

EntityManagerFactory emf  = Persistence.createEntityManagerFactory(PU_NAME);
EntityManager entityManager = emf.createEntityManager();

Note that creating an EntityManagerFactory is a costly operation and should not be done for each request. However, creating an EntityManager is not and you should get one for each thread. But in your case, I'd suggest to use the struts2-persistenceplugin to handle this.

Thanks, but it seems [...] that Java EE is not mandatory to use injection [...] the Spring brings necessary engine for it.

Indeed. But you wrote "NO spring at all" in your other question and you didn't list any piece that could provide injection out of the box. Anyway, check out the struts2-persistenceplugin, it might be enough for your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文