zawisza017
zawisza017

Reputation: 21

How to inject @PersistenceContext into pojo class

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 )

Mamba.Core

Upvotes: 2

Views: 17686

Answers (4)

Greg
Greg

Reputation: 1

I have the same issue. My SLSB injects my DAO ojbect with @Inject. The @PersistenceContext is in the POJO. When the POJO is in the same maven project as the EJB, everything works fine. Im not sure why, but the EJB cannot inject the POJO (w/ PU) when it is in a different project, unless I make the POJO a SLSB and use @EJB instead of @Inject.

Upvotes: 0

sola
sola

Reputation: 1576

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.

Upvotes: 2

Ot&#225;vio Garcia
Ot&#225;vio Garcia

Reputation: 1372

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.

Upvotes: 0

Bozho
Bozho

Reputation: 597106

  • first, your pojo needs to be in a bean archive (have beans.xml in META-INF or WEB-INF) in order to be managed
  • @PersistenceContext requires a META-INF/persistence.xml, where you define a persistent unit
  • if there is a persistent unit and it still fails, try @PersistenceContext(unitName="name")

Upvotes: 1

Related Questions