2
votes

Whenever I use CDI managed beans in my Enterprise Application Project, I get the CDI deployment failure:WELD-001408 exception. This is what I get:

Injection method name must start with "set"
 symbol: javax.persistence.PersistenceUnit
 location: public javax.persistence.EntityManager de.syngenio.backend.beans.Resources.create(javax.persistence.EntityManagerFactory)

and the exception:

Exception during lifecycle processing
org.glassfish.deployment.common.DeploymentException: CDI deployment failure:WELD-001408: Unsatisfied dependencies for type EntityManagerFactory with qualifiers @Default
  at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedMethod] @Produces @ConversationScoped @PersistenceUnit public de.syngenio.backend.beans.Resources.create(EntityManagerFactory)
  at de.syngenio.backend.beans.Resources.create(Resources.java:0)

This is the class:

public class Resources {

    @Produces
    @ConversationScoped
    @PersistenceUnit(unitName = "mongodb-pu")
    public EntityManager create(EntityManagerFactory emf) {
        return emf.createEntityManager();
    }

    public void close(@Disposes EntityManager em) {
        em.close();
    }
}

I inject with

@Inject
private EntityManager em;

I'm using Glassfish 4.1 and JDK build 1.8.0_25-b17

edit:

META-INF/beans.xml (EJB project):

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                               http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
           version="1.1" bean-discovery-mode="all">
    </beans>

META-INF/persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="mongodb-pu" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <properties>
        <property name="eclipselink.target-database" value="org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform"/>
        <property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec"/>
        <property name="eclipselink.nosql.property.mongo.port" value="27017"/>
        <property name="eclipselink.nosql.property.mongo.host" value="localhost"/>
        <property name="eclipselink.nosql.property.mongo.db" value="webshop"/>
        <property name="eclipselink.logging.level" value="FINEST"/>
    </properties>
</persistence-unit>
</persistence>
1
I think you are missing beans.xml file in your webapp/WEB-INF/ folder. For CDI to work this file is needed even if empty e.g. gist.github.com/sejersbol/845053rangalo
Actually it's there. Sorry, I missed to write that. I have a beans.xml in META-INF/ of the EJB-project. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" version="1.1" bean-discovery-mode="all"> </beans>mherwig
Can you also paste your persistence.xml where you declare your persistence unit ? Is the datasource created in gf ? Are there other exceptions related to db ?rangalo
I updated the original post. I use eclipselink and the eclipselink nosql extension. I placed the extension in the glassfish modules folder plus the mongo java driver. Actually the database connection works just fine if I create a entitymanager instance with the entitymanagerfactory from a stateless session bean.mherwig
What is your deployment unit war or ear ?rangalo

1 Answers

0
votes

When Using EntityManagerFactory as a Parameter in managed methods, you should ensure that EntityManagerFactory would be injectable too. Because your method "create" is managed, all parametervalues where created in a similar way like an @Inject. I don't know why your EntityManagerFactory is not injectable nor if this is the right way to obtain an instance of it. My suggestion would be: 1. To obtain your EntityManagerFactory in another way in your create-method 2. Or to write a producer for your EntityManagerFactory which creates and returns a new EntityManagerFactory.