简体   繁体   中英

How do I inject EntityManager into a EJB using Bluemix Liberty runtime?

It was supposed to be a trivial thing to do and it's described here (which injects EntityManagerFactory and not EntityManager ), here (which does not inject EntityManager , but retrieves it from Persistence object) and here (which actually injects EntityManager in the way I want, and what it says makes sense, since it assumes the data source is called jdbc/<yourdbname> )

However, the third one has a broken link because the source code is not available any more, unfortunately.

My persistence.xml uses JPA 2.1

<?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="mycogdb">
        <jta-data-source>jdbc/cogdb</jta-data-source>
        <properties>
            <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
            <property name="eclipselink.ddl-generation" value="create-tables" />
        </properties>
    </persistence-unit>
</persistence>

and my EJB could not be any simpler

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class MyEJB {

     @PersistenceContext(unitName="mycogdb")
     private EntityManager em;

    public void test() {
        MyEntity me = new MyEntity();
        me.setId(System.currentTimeMillis());
        em.persist(me);
    }
}

However, it's not being injected

    [ERROR ] CWWJP0029E: The server cannot find the <default> persistence 
unit in the myapp.war module and the myapp application.
    2016-06-17T08:39:02.541-0300
    [App/0]
    out
    [INFO ] FFDC1015I: An FFDC Incident has been created: 
"com.ibm.wsspi.injectionengine.InjectionException: 
The java:comp/env/mycogdb reference of type javax.persistence.EntityManager 
for the null component in the myapp.war module of the myapp application 
cannot be resolved. com.ibm.ejs.container.ManagedBeanOBase.injectInstance 
134" at ffdc_16.06.17_11.39.02.0.log

Am I missing any configuration in server.xml or in any other config file? Maybe any missing server feature to be enabled? Any help is welcome.

[Edited: Originally noted that the annotation attribute is unitName not name but that ended up not being the problem.]

However the build is performed, make sure the end result is that the persistence.xml ends up in location WEB-INF/classes/META-INF/persistence.xml within the WAR archive.

This is mentioned in JPA Tutorial :

If you package the persistence unit as a set of classes in a WAR file, persistence.xml should be located in the WAR file's WEB-INF/classes/META-INF directory.

Now, that still might leave some questions regarding how to structure the project from both the Eclipse/WDT and Maven angles, as well as integration of your Maven build with the DevOps tool which has Maven integration .

I won't attempt to give a complete answer here across all options, but just noting that it might be more complicated than saying "always put your persistence.xml in src/main/resources " since src/main/java/META-INF appears to me to work in some build use cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM