如何将@PersistenceContext注入到pojo类中
我正在尝试使用 @PersistenceContex 注释将 PersistenceContex 注入 POJO 中,我读到我需要让 POJO 设法做到这一点。所以我将我的 POJO 类注入到 servlet 中(所以它现在作为依赖对象进行管理,我是对的吗?)但是当 servlet 尝试从注入的对象调用方法时,我收到错误:
java.lang.IllegalStateException: 无法检索 EntityManagerFactory 为 null
所以看起来 PersistenceContext 没有正确注入到 POJO 中,我应该怎么做才能使它工作?
我的 POJO 类如下所示:
public class FileEntityControlerImpl implements FileEntityInterface {
@PersistenceContext
EntityManager entityManager;
@Override
public void createFile(FileEntity fileEntity) {
...}
@Override
public FileEntity retriveFile(String fileName) {
...}
注入点:
@Inject
FileEntityInterface fileController;
如果我使用 SLSB 并使用 @EJB 注入,它可以正常工作。
..::更新::..
堆栈跟踪:
警告:StandardWrapperValve[ResourcesServlet]:PWC1406:servlet ResourcesServlet 的 Servlet.service() 引发异常 java.lang.IllegalStateException:无法检索unitName MambaPU的EntityManagerFactory 在 com.sun.enterprise.container.common.impl.EntityManagerWrapper.init(EntityManagerWrapper.java:121) 在 com.sun.enterprise.container.common.impl.EntityManagerWrapper._getDelegate(EntityManagerWrapper.java:162) 在 com.sun.enterprise.container.common.impl.EntityManagerWrapper.createNamedQuery(EntityManagerWrapper.java:554) 在pl.zawi.mamba.core.integration.controllers.implementation.FileEntityControlerImpl.retriveFile(FileEntityControlerImpl.java:32) 在 pl.zawi.mamba.core.face.servlets.ResourcesServlet.doGet(ResourcesServlet.java:60) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:734) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 在 org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188) 在 org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641) 在 com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97) 在 com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185) 在 org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226) 在 com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165) 在com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791) 在 com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693) 在 com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954) 在 com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170) 在 com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135) 在 com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102) 在 com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88) 在 com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76) 在com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53) 在 com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57) 在com.sun.grizzly.ContextTask.run(ContextTask.java:69) 在 com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330) 在 com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309) 在 java.lang.Thread.run(Thread.java:662)
persistance.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="MambaPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/MambaDB</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<shared-cache-mode>ALL</shared-cache-mode>
<properties>
<!-- <property name="javax.persistence.jdbc.password" value="root"/>-->
<!-- <property name="javax.persistence.jdbc.user" value="root"/>-->
<!-- <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>-->
<!-- <property name="eclipselink.ddl-generation" value="create-tables"/>-->
<!-- <property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/>-->
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
..::UPDATE2::..
如果有人感兴趣,我的项目有来源,我一直在使用 Maven,所以它应该很简单构建并运行。(MySql 驱动器不包含在 pom-s 中,因此请记住这一点)
I'm trying to inject PersistenceContex into POJO using @PersistenceContex annotation, I've read that i need to made that POJO managed to do that. So I inject my POJO class into servlet(so its now managed as dependent object, am i right ?) but when servlet is trying to call metod from injected object i get error:
java.lang.IllegalStateException:
Unable to retrieve
EntityManagerFactory for unitName null
So it looks like PersistenceContext is not injected into POJO properly, what should I do to make it work ?
My POJO class looks like this:
public class FileEntityControlerImpl implements FileEntityInterface {
@PersistenceContext
EntityManager entityManager;
@Override
public void createFile(FileEntity fileEntity) {
...}
@Override
public FileEntity retriveFile(String fileName) {
...}
Injection point:
@Inject
FileEntityInterface fileController;
If I use SLSB and inject using @EJB it works fine.
..::UPDATE::..
stacktrace:
WARNING: StandardWrapperValve[ResourcesServlet]: PWC1406: Servlet.service() for servlet ResourcesServlet threw exception
java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName MambaPU
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.init(EntityManagerWrapper.java:121)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper._getDelegate(EntityManagerWrapper.java:162)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createNamedQuery(EntityManagerWrapper.java:554)
at pl.zawi.mamba.core.integration.controllers.implementation.FileEntityControlerImpl.retriveFile(FileEntityControlerImpl.java:32)
at pl.zawi.mamba.core.face.servlets.ResourcesServlet.doGet(ResourcesServlet.java:60)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
at java.lang.Thread.run(Thread.java:662)
persistance.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="MambaPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/MambaDB</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<shared-cache-mode>ALL</shared-cache-mode>
<properties>
<!-- <property name="javax.persistence.jdbc.password" value="root"/>-->
<!-- <property name="javax.persistence.jdbc.user" value="root"/>-->
<!-- <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>-->
<!-- <property name="eclipselink.ddl-generation" value="create-tables"/>-->
<!-- <property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/>-->
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
..::UPDATE2::..
If someone is interested there is sorce of my project, I've been using maven so it should be simple to build and run.(MySql drive is not included in pom-s so keep it in mind )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
仅供参考:
您根本不在实体类上使用 @PersistenceContext 注释。只需在 POJO 中包含一个持久性单元即可对它们进行管理(将 persistence.xml 和空 beans.xml 添加到 POJO 类的 JAR 的 META-INF 文件夹中。
@PersistenceContext 用于会话 Bean,其目的是自动将 EntityManager 注入到会话 bean 中。
Just for the reference:
You don't use the @PersistenceContext annotation at all on Entity classes. Simply including a Persistence Unit with the POJOs will make them managed (adding a persistence.xml and an empty beans.xml into the META-INF folder of the JAR of the POJO classes.
@PersistenceContext is used on Session Beans and its purpose is to automatically inject the EntityManager into the session bean.
META-INF
或WEB-INF
中具有beans.xml
),以便托管@PersistenceContext
需要一个META-INF/persistence.xml
,您可以在其中定义一个持久单元,@PersistenceContext (单位名称=“名称”)
beans.xml
inMETA-INF
orWEB-INF
) in order to be managed@PersistenceContext
requires aMETA-INF/persistence.xml
, where you define a persistent unit@PersistenceContext(unitName="name")
我有同样的问题: Glassfish 不启动EntityManager 如果 DAO 不是无状态的
我认为这是 Glassfish 问题,因为在 JBoss AS 6 下工作正常。
I have the same issue: Glassfish doesn't bring up EntityManager if DAO is not Stateless
I think that is a Glassfish issue, because works fine under JBoss AS 6.
我有同样的问题。我的
SLSB
使用@Inject
注入我的DAO
对象。@PersistenceContext
位于 POJO 中。当 POJO 与 EJB 位于同一个 Maven 项目中时,一切正常。我不知道为什么,但是当 EJB 位于不同的项目中时,它无法注入 POJO(带 PU),除非我将 POJO 设为 SLSB 并使用@EJB
而不是@Inject
。I have the same issue. My
SLSB
injects myDAO
ojbect with@Inject
. The@PersistenceContext
is in the POJO. When the POJO is in the same maven project as theEJB
, everything works fine. Im not sure why, but theEJB
cannot inject the POJO (w/ PU) when it is in a different project, unless I make the POJO aSLSB
and use@EJB
instead of@Inject
.