为什么 JPA 注入在 @PersistentUnit 上不起作用
这是问题的继续( 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Tomcat 不会注入任何非 Java EE 容器的内容(即使使用 Java EE 5 容器,注入也仅适用于托管组件,如 servlet、过滤器、侦听器、EJB、Web 服务端点...)。因此,您必须手动创建
EntityManagerFactory
(通常在 servlet 或帮助程序类中)并从中获取EntityManager
:请注意,创建
EntityManagerFactory
code> 是一项成本高昂的操作,不应为每个请求都执行此操作。但是,创建 EntityManager 则不然,您应该为每个线程创建一个。但就您而言,我建议使用 struts2-persistenceplugin 来处理此问题。的确。但是您在其他问题中写了“根本没有弹簧”,并且您没有列出任何可以提供开箱即用注射的部件。无论如何,请查看 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 theEntityManager
from it:Note that creating an
EntityManagerFactory
is a costly operation and should not be done for each request. However, creating anEntityManager
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.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.