Java EntityManager为null,带有@PersistenceContext [英] Java EntityManager null with @PersistenceContext

查看:123
本文介绍了Java EntityManager为null,带有@PersistenceContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jersey的Wildfly 10,并通过@Inject注入依赖项.我有DAO和Service接口,并在CustomBinder中声明了它们的实现.注入效果很好,但是 EntityManager 被注入 null 并带有 @PersistenceContext 批注.我正在使用MySQL,并且数据源测试连接正在工作.

I'm using Wildfly 10, Jersey, and injecting dependencies with @Inject. I have DAO and Service interfaces with their implementations declared in a CustomBinder. The injections work well, but the EntityManager is injected null with the @PersistenceContext annotation. I'm using MySQL and the datasource test-connection is working.

API Rest类

@Path("/account")
public class CuentaServiceRS {

    @Inject
    private ICuentaService cuentaService;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Cuenta getCuenta() {
        return cuentaService.getCuentas().get(0);       
    }

}

ICuentaService

@Stateless
public class CuentaServiceImpl implements ICuentaService {

    @Inject
    private ICuentaDAO cuentaDAO;

    @Override
    public List<Cuenta> getCuentas() {
        List<Cuenta> cuentas = cuentaDAO.getAllCuentas();
        cuentas;
    }

}

CuentaDAO

@Stateless
public class CuentaDAOImpl implements ICuentaDAO {

    @PersistenceContext(unitName = "altitudePU")
    protected EntityManager em;

    @Override
    public List<Cuenta> getAllCuentas() {
        CriteriaQuery<Cuenta> cq = em.getCriteriaBuilder().createQuery(Cuenta.class);

        /...

        return resultlist;
    }

}

我在 persistence.xml

<persistence-unit name="altitudePU">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:jboss/datasources/AltitudePU</jta-data-source>
    <class>ar.com.olx.domain.Cuenta</class>
    <properties>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
    </properties>    
</persistence-unit>

web.xml

<servlet>
        <servlet-name>altitudeservlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>ar.com.villat.bind.ApplicationJaxRS</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>

我的自定义 AplicationJaxRS ,它从ResourceConfig扩展而来

My custom AplicationJaxRS which extendes from ResourceConfig

public class ApplicationJaxRS extends ResourceConfig {

    public ApplicationJaxRS(){
        register(new CustomBinder());
        packages(true, "ar.com.olx");
    }

}

CustomBinder

public class CustomBinder extends AbstractBinder {

    @Override
    protected void configure() {
        bind(CuentaServiceImpl.class).to(ICuentaService.class);
        bind(CuentaDAOImpl.class).to(ICuentaDAO.class);
    }

}

最后,我与该帖子有关的 pom.xml 依赖项

And finally, my pom.xml dependencies related to this post

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.22.2</version>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>javax.enterprise</groupId>
    <artifactId>cdi-api</artifactId>
    <version>1.2</version>
</dependency>

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>3.5.6-Final</version>
</dependency>

<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.0.Final</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>org.jboss.spec.javax.ejb</groupId>
    <artifactId>jboss-ejb-api_3.2_spec</artifactId>
    <version>1.0.0.Final</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
</dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>

<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>1.6.1</version>
    <scope>provided</scope>
</dependency>

如果您需要更多信息,请告诉我.

If you need more information, please tell me.

推荐答案

当您使用全栈JavaEE服务器时,可以大大简化这一过程.

As you are using a full stack JavaEE server you can simplify this a lot.

如果您尚未将MySQL数据源添加到WildFly,则可以这样做:

If you have not added a MySQL datasource to WildFly then you can do it like this:

  1. 创建一个名为deploy-mysql-ds.cli的文件,其内容如下:

  1. Create a file called deploy-mysql-ds.cli with the following content:

# Execute offline
embed-server --server-config=standalone.xml

deploy $HOME/.m2/repository/mysql/mysql-connector-java-5.1.39.jar

# Add the application datasource
data-source add \
    --name=AltitudeDS \
    --driver-name=mysql-connector-java-5.1.39.jar \
    --connection-url=jdbc:mysql://localhost:3306/altitudeDB \
    --jndi-name=java:jboss/datasources/AltitudePU \
    --user-name=$USER_NAME \
    --password=$PASSWORD

  • 用实际值替换$HOME$USER_NAME$PASSWORD

    通过像下面这样执行来在Wildfly中配置数据源:

    Configure the data source in Wildfly by executing it like:

    $JBOSS_HOME/bin/jboss-cli.sh --file="deploy-mysql-ds.cli"
    

  • 您只需要执行一次.

    请注意,--jndi-name与中的<jta-data-source>...</jta-data-source>相匹配 您的persistence.xml文件.

    Note that the --jndi-name matches the <jta-data-source>...</jta-data-source> in your persistence.xml file.

    您不需要在maven依赖项中使用mysql-connector-java-5.1.39.jar,因为它已直接添加到服务器中.还有其他解决方案,涉及将jar添加为模块" 但我发现这种方法要简单得多.

    You do not need the mysql-connector-java-5.1.39.jar in your maven dependencies because it has been added to the server directly. There are other solutions that involve adding the jar as a "module" but I find this way to be much simpler.

    如下修改您的应用程序:

    Amend your application as follows:

    1. 删除所有冗余依赖项:

    1. Remove all the redundant dependencies:

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>
    

  • 删除AplicationJaxRSCustomBinder类并添加JAXRSConfiguration类:

  • Remove the AplicationJaxRS and CustomBinder classes and add a JAXRSConfiguration class:

    /**
     * Configures a JAX-RS endpoint
     */
    @ApplicationPath("resources")
    public class JAXRSConfiguration extends Application {
    }
    

  • 从web.xml中删除altitudeservlet.如果只有它,则完全删除整个文件.

  • Remove the altitudeservlet from the web.xml. If that is the only thing in it then completely remove the whole file.

    测试

    您应该能够部署此Web应用程序并从以下位置访问它:

    Test it

    You should be able to deploy this web application and access it from:

    http://localhost:8080/altitude-webapp/resources/account
    

    其中altitude-webapp是您的WAR文件的名称.

    where altitude-webapp is the name of your WAR file.

    这篇关于Java EntityManager为null,带有@PersistenceContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆