2

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

zawisza017
  • 21
  • 1
  • 1
  • 4

4 Answers4

2

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.

sola
  • 1,498
  • 14
  • 23
1
  • 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")
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • beans.xml is present in all modules of my ear file but there's nothing special inside, and yes persistence.xml is also present. I have already try to add unitName, and name, but error is steal the same :( – zawisza017 Feb 17 '11 at 14:03
  • @zawisza017 can you give the whole stacktrace – Bozho Feb 17 '11 at 14:10
  • I have add stacktrace and persistance.xml to main post. My ear file consists of 3 modules one web and two ejb modules one of them contains persistance.xml – zawisza017 Feb 17 '11 at 14:29
  • @zawisza017 I'm not sure if the jpa unit can span multiple modules. try to define it in the desired module. – Bozho Feb 17 '11 at 15:08
  • When I Inject SLSB whit PU it works. I have 3 modules face.war where the servlet is, integration.jar where SLSB with injectet Entity Manager are, and logic.jar where i have singleton which use some of integration module SLSB to fill database. My goal was to replace that SLSB with managet POJO creating kind of DAO, as people on stackoverflow advise. – zawisza017 Feb 17 '11 at 16:52
  • @zawisza017 that's strange. Perhaps grab the latest version of CDI (or the application server you are using) – Bozho Feb 17 '11 at 17:02
  • already tried glassfish 3.1b40 or something around - same mistake. I have no problem placing here my full project if someone would want to try it himself. – zawisza017 Feb 17 '11 at 17:13
  • @zawisza017 how about trying it on JBoss AS 6? Garcia noted that it works there. Give it a try – Bozho Feb 17 '11 at 17:27
  • I'm currently downloading it, I have relay poor quality connection – zawisza017 Feb 17 '11 at 17:36
  • I have downloaded Jboss but it looks like i need to read something about howto make my app deploy... because first attemption to deploy end up with lots of errors... unfortunately i have not time to do that :( – zawisza017 Feb 17 '11 at 18:54
  • @zawisza017 quite bad indeed. After a handful of these experiences I decided not to use applications servers at all, so I've been using Tomcat only for a long time :) – Bozho Feb 17 '11 at 18:57
0

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.

Jean-Philippe Bond
  • 10,089
  • 3
  • 34
  • 60
Greg
  • 1
0

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.

Community
  • 1
  • 1
Otávio Garcia
  • 1,372
  • 1
  • 15
  • 27
  • Any one else have this king of problem, i do not know JBoss at all it bad new if I'm forced to use it;/ – zawisza017 Feb 17 '11 at 15:20
  • If you don't like JBoss AS, try to use an empty Stateless Session Bean that has a private @PersistenceContext EntityManager em. This forces Glassfish to initialize persistence unit. For me this solution works fine. It's not a good solution, I know, but works. I'll do more tests to report this issue to Glassfish team. – Otávio Garcia Feb 17 '11 at 16:16
  • my PU i already initialized, moreover before servlet is created i use singleton to fill database with some content(from that i know that PU + SLSB works fine when injecting between modules) – zawisza017 Feb 17 '11 at 16:38
  • Like Bozho, I don't know if @Inject can be inject beans from other module. I can't find about this in the spec (JSR299). – Otávio Garcia Feb 17 '11 at 16:45
  • simple injection using @Inject between modules works fine i have already checked it, by creating simple one method(print) Test interface and TestImpl, then injecting it to another module, however that was between two EJB modules – zawisza017 Feb 17 '11 at 16:57