EntityManagerFactory为null

时间:2017-10-29 13:39:04

标签: hibernate jax-rs wildfly resteasy

我一直在研究RESTEasy项目,似乎无法注入entityManager。

我有野生动物和mysql。

这是我测试与Wildfly连接时的数据源

Name: Test  
JNDI: java:/Test
Driver: mysql-connector-java-5.1.33.jar_com.mysql.jdbc.Driver_5_1

这是我的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="Test" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/Test</jta-data-source>
        <properties>
             <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
             <property name="hibernate.show_sql" value="true" />
           <property name="hibernate.archive.autodetection" value="class, hbm"/>
        </properties>
    </persistence-unit>
</persistence>

这是我想使用entityManagerFactory的地方。

import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.ws.rs.*;
@Path("/test")
public class TestApi  {
@PersistenceUnit(unitName = "Test")
private EntityManagerFactory entityManagerFactory;

@GET
@Path("/")
public Response Action(@Context SecurityContext securityContext)
throws NotFoundException {
        Action action = new Action();
        action.setGameId(1234);
        action.setScore(90);
        action.setUserId(321);
        action.setType("test");
        em.persist(action);
}

我尝试了应用程序管理的方法,并让它工作,但我真的希望它是容器管理的,不能让它工作。我错过了什么吗?

0 个答案:

没有答案