0

im facing following problem. In my application I'm trying to use @PersistenceContext injected into the NON ejb instance of DAO by Glassfish embedded server. So it's a pojo controlled by CDI. Like this :

@Named
public class DummyDAO implements Serializable {
    @PersistenceContext private EntityManager entityManager;

    public void testManager() {

        if (entityManager == null) {
            throw new RuntimeException("It's null!!");
        }
        System.out.println("Hello! I'm injected");
    }
}

This dummy DAO injects into a JSF bean by CDI like this :

@ManagedBean
@SessionScoped
public class OrderImportManagerBean implements Serializable {


    @Inject
    DummyDAO dummyDAO;

    public void testManager() {
        dummyDAO.testManager();
    }
}

testManager() called from an xhtml page like #{orderImportManagerBean.testManager}

entityManager is always null. The CDI itself works, all instances injected as they should but not this one. I have persistence.xml and orm.xml under resources/META-INF catalog. So after the project is packaged I get META-INF and stuff in right place (classes/META-INF). Here is my persistence.xml :

<persistence 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"
             version="2.0">
    <persistence-unit name="persistance-unit" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testdb"/>
            <property name="javax.persistence.jdbc.user" value="sa"/>
            <property name="javax.persistence.jdbc.password" value=""/>
            <property name="hibernate.dialect"
                      value="org.hibernate.dialect.HSQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
            <property name = "hibernate.show_sql" value = "true" />
        </properties>
    </persistence-unit>
</persistence>

Now I'm wondering what is wrong with my configuration? Any suggestions?

Thanks.

Anton
  • 1,001
  • 9
  • 23
  • Maybe you can't inject a PersistenceContext into a base class and have it work for inherited classes? That's always how I thought it worked when using a container. For example, I always use an abstract method _getEntityManager()_ in the base class and inject the EntityManager in all the child classes (which actually get created by the container). – jahroy Jun 08 '14 at 18:42
  • @jahroy not quite. I tried as you proposed and created a brand new dao with no parent and PersistenceContext field. But it was not injected either. I can't see any trace of injection in logs of the server. Seems like the annotation ignored completely. Some kind of misconfiguration. – Anton Jun 08 '14 at 19:18
  • Right on... figured it was worth a try. – jahroy Jun 08 '14 at 19:20
  • Changed the questuion as @perissf asked, it's even simplier now. – Anton Jun 09 '14 at 11:05
  • I am not able to reproduce the problem in standard GF 3.1 installation. Can you try yourself, just to exclude some possible causes? – perissf Jun 09 '14 at 16:34
  • @perissf could you share your version on github? – Anton Jun 09 '14 at 18:34
  • No, I won't. I am using your code, in a brand new project, except persistence.xml that has been autogenerated by my IDE NetBeans and beans.xml to enable CDI. The entity manager is injected successfully and I get the Hello message. – perissf Jun 09 '14 at 18:56
  • Which hibernate version are you using? I tried to start application in JBOSS an got some trace : Internal Server Error { "outcome" => "failed", "failure-description" => {"JBAS014671: Failed services" => {"jboss.persistenceunit.\"webapp.war#persistance-unit\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"webapp.war#persistance-unit\": java.lang.IncompatibleClassChangeError: Implementing class Caused by: java.lang.IncompatibleClassChangeError: Implementing class"}}, "rolled-back" => true } – Anton Jun 09 '14 at 19:01
  • @perissf could you please share your version of the app? It may be so that you are using some specific configuration that works on glassfish. As for me I'm using Hibernate 4.3.5 with JPA 2.1 support. My beans are in a separate jar and have no annotations. That's why I have orm.xml. This issue seems to be a jarhell issue. So I need to know which configuration actualy works. Thanks. – Anton Jun 10 '14 at 07:08

0 Answers0