Wildfly Swarm EntityManager为null

时间:2016-03-26 16:52:20

标签: java jpa wildfly microservices wildfly-swarm

我有一个微服务,它是一个调度程序。我通过REST json获得了一个计时工作。 我按照一些关于如何添加persistence.xml的教程,但它似乎没有用。我没有收到任何失败消息,但是来自EntityManager的NullpointerException。

它没有创建表格等所以我认为添加persistence.xml也有问题吗?

连接数据源没有问题。我测试了它,似乎工作正常。

SchedulerMain:

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.ClassLoaderAsset;
import org.wildfly.swarm.Swarm;
import org.wildfly.swarm.container.Container;
import org.wildfly.swarm.datasources.DatasourceArchive;
import org.wildfly.swarm.jaxrs.JAXRSArchive;

public class SchedulerMain {

public static void main(String[] args) {

    try {
        Container container = new Container();
        container.start();
        container.deploy(Swarm.artifact("mysql:mysql-connector-java", "mysql"));

        deployApp(container);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static void deployApp(Container container) throws Exception {
    JAXRSArchive app = ShrinkWrap.create(JAXRSArchive.class);

    app.as(DatasourceArchive.class).dataSource("Scheduler", (ds) -> {
        ds.connectionUrl("jdbc:mysql://localhost:3306/scheduler");
        ds.driverName("mysql");
        ds.userName("root");
        ds.password("");
    });

    app.addAsWebInfResource(new ClassLoaderAsset("META-INF/persistence.xml", SchedulerMain.class.getClassLoader()),
            "classes/META-INF/persistence.xml");

    app.addAsLibrary(container.createDefaultDeployment());
    app.addAllDependencies();

    container.deploy(app);
}
}

ScheduleRESTResource:

@ApplicationScoped
@Path("/schedule")
public class ScheduleRESTResource {

    @Inject
    private SchedulerController schedulerController;

    @Inject
    private ScheduleBean scheduleBean;

    @POST
    @Path("/job")
    @Consumes(MediaType.APPLICATION_JSON)
    public void schedule(MySchedule schedule) throws IOException {
        List<MySchedule> schedules = scheduleBean.getAllSchedules();
        System.out.println(schedules.toString());
        schedulerController.buildSchedule(schedule);
    }
}

ScheduleBean:

import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;

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

import de.fszm.scheduler.entities.MySchedule;

@Stateless
public class ScheduleBean {

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

    public List<MySchedule> getAllSchedules() {
        System.out.println("getAllSchedules");

        List<MySchedule> schedules = new ArrayList<>();

        if (em != null) {
            schedules = em.createNamedQuery("Schedule.getAllSchedules",     MySchedule.class).getResultList();
        } else {
            System.out.println("em is null");
        }

        return schedules;
    }
}

的persistence.xml:

<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="Scheduler" transaction-type="JTA">

    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <jta-data-source>java:jboss/datasources/Scheduler</jta-data-source>

    <properties>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/scheduler"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.connection.username" value="root"/>
        <property name="hibernate.connection.password" value=""/>
        <property name="hibernate.archive.autodetection" value="class"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.hbm2ddl.auto" value="drop-and-create"/>
    </properties>
</persistence-unit>

的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>...</groupId>
<artifactId>scheduler</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>scheduler</name>
<packaging>jar</packaging>

<properties>
    <version.swarm>1.0.0.Beta6</version.swarm>
    <version.mysql>5.1.6</version.mysql>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.wildfly.swarm</groupId>
            <artifactId>bom</artifactId>
            <version>${version.swarm}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>undertow</artifactId>
    </dependency>

    <dependency>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>jaxrs</artifactId>
    </dependency>

    <dependency>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>ejb</artifactId>
    </dependency>

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

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

    <dependency>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>jaxrs-weld</artifactId>
    </dependency>

    <dependency>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>datasources</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${version.mysql}</version>
    </dependency>

<!-- <dependency>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>wildfly-swarm-ejb</artifactId>
        <version>1.0.0.Alpha5</version>
    </dependency> -->

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.wildfly.swarm</groupId>
            <artifactId>wildfly-swarm-plugin</artifactId>
            <version>${version.swarm}</version>
            <configuration>
            </configuration>
            <executions>
                <execution>
                    <id>package</id>
                    <goals>
                        <goal>package</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin> 
    </plugins>
</build>

0 个答案:

没有答案