如何将@PersistenceContext注入pojo类

时间:2011-02-17 13:30:36

标签: java-ee-6 code-injection cdi

我正在尝试使用@PersistenceContex注释将PersistenceContex注入POJO,我读过我需要让POJO设法做到这一点。所以我将我的POJO类注入servlet(所以它现在作为依赖对象管理,我是对的吗?)但是当servlet试图从注入的对象调用metod时我得到错误:

  

java.lang.IllegalStateException:   无法检索   UnitName null的EntityManagerFactory

所以看起来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进行注入,则可以正常工作。

.. :: UPDATE :: ..

堆栈跟踪:

  

警告:StandardWrapperValve [ResourcesServlet]:PWC1406:Servlet的Servlet.service()ResourcesServlet抛出异常   java.lang.IllegalStateException:无法检索unitName MambaPU的EntityManagerFactory           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)           在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)           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)           在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)           在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)           at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)           在com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)           at 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中,所以请记住)

Mamba.Core

4 个答案:

答案 0 :(得分:2)

仅供参考:

您根本不在Entity类上使用@PersistenceContext注释。简单地将持久性单元与POJO一起包含就可以对它们进行管理(将persistence.xml和空beans.xml添加到POJO类的JAR的META-INF文件夹中。

@PersistenceContext用于Session Beans,其目的是自动将EntityManager注入会话bean。

答案 1 :(得分:1)

  • 首先,你的pojo需要在一个bean存档中(在beans.xmlMETA-INF中有WEB-INF)才能被管理
  • @PersistenceContext需要META-INF/persistence.xml,您可以在其中定义持久单元
  • 如果有持久性单位且仍然失败,请尝试@PersistenceContext(unitName="name")

答案 2 :(得分:0)

我遇到同样的问题:Glassfish doesn't bring up EntityManager if DAO is not Stateless

我认为这是一个Glassfish问题,因为在JBoss AS 6下工作正常。

答案 3 :(得分:0)

我有同样的问题。我的SLSBDAO ojbect注入@Inject@PersistenceContext位于POJO中。当POJO与EJB处于同一个maven项目时,一切正常。我不知道为什么,但EJB在不同的项目中不能注入POJO(w / PU),除非我将POJO设为SLSB并使用@EJB代替{ {1}}。