I have a stateless EJB that injects

@PersistenceUnit 
private EntityManagerFactory factory

My persistence.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="ejbPU-ro" transaction-type="RESOURCE_LOCAL">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <non-jta-data-source>mySQLDataSource</non-jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="openjpa.jdbc.DBDictionary" value="mysql"/>
            <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(foreignKeys=true"/>
            <property name="openjpa.jdbc.SchemaFactory" value="native(foreignKeys=true)"/>
            <property name="openjpa.Log" value="DefaultLevel=INFO,SQL=INFO"/>
        </properties>
    </persistence-unit>
</persistence>

When unit testing I'm using:

Properties p = new Properties();
p.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory");
p.setProperty(Context.PROVIDER_URL, "http://127.0.0.1:4204/openejb/ejb");
InitialContext initialContext = new InitialContext(p);

Also defining the openejb.home VM arg and the unit-test runs perfectly.

When deploying to TomEE I have the persistence.xml in my.war/WEB-INF/lib/my.jar/META-INF.

in conf/tomee.xml I have:

<JndiProvider id="prov" type="javax.naming.InitialContext">
    java.naming.factory.initial = org.apache.openejb.core.LocalInitialContextFactory
    java.naming.provider.url = http://127.0.0.1:4204/openejb/ejb
</JndiProvider>

and:

<Resource id="mySQLDataSource" type="DataSource">
    JdbcDriver          com.mysql.jdbc.Driver
    JdbcUrl             jdbc:mysql://127.0.0.1:3306/mydb
    UserName            root
    Password        
        JtaManaged      false
        DefaultAutoCommit   false
</Resource>

The log looks fine but when I run the code the injected factory is null.

Any ideas?

有帮助吗?

解决方案

several stuff:

1) your initial context:

p.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.apache.openejb.core.LocalInitialContextFactory");
p.setProperty(Context.PROVIDER_URL, "http://127.0.0.1:4204/openejb/ejb"); <- useless since you are in embedded mode

2) your web.xml has metadata-complete=true so scanning is inhibited (remove it)

3) you don't need org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet normally (see http://svn.apache.org/repos/asf/tomee/tomee/trunk/examples/rest-example-with-application/) - or use the JAXRS trick to declare it through web.xml (IIRC javax.ws.rs.Application as init parameter)

4) not sure your ajax url is right (on the provided sample on tomee@)

finally got a not null emf (tested on trunk since it was here ;)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top