在 eclipse 中使用 wildfly 的 postgres 数据库

postgres database with wildfly in eclipse

我在使用 Eclipse 的动态 Web 项目中设置数据库连接时遇到了问题。 我向 Wildfly 添加了一个数据源,所以 standalone.xml 看起来像这样:

<datasource jta="true" jndi-name="java:jboss/datasources/weathermonitor" pool-name="weathermonitor" enabled="true" use-ccm="true">
                <connection-url>jdbc:postgresql://localhost:5432/weathermonitor</connection-url>
                <driver-class>org.postgresql.Driver</driver-class>
                <driver>postgres</driver>
                <security>
                    <user-name>postgres</user-name>
                    <password>student</password>
                </security>
                <validation>
                    <validate-on-match>false</validate-on-match>
                    <background-validation>false</background-validation>
                </validation>
                <timeout>
                    <set-tx-query-timeout>false</set-tx-query-timeout>
                    <blocking-timeout-millis>0</blocking-timeout-millis>
                    <idle-timeout-minutes>0</idle-timeout-minutes>
                    <query-timeout>0</query-timeout>
                    <use-try-lock>0</use-try-lock>
                    <allocation-retry>0</allocation-retry>
                    <allocation-retry-wait-millis>0</allocation-retry-wait-millis>
                </timeout>
                <statement>
                    <share-prepared-statements>false</share-prepared-statements>
                </statement>
            </datasource>
            <drivers>
                <driver name="h2" module="com.h2database.h2">
                    <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
                </driver>
                <driver name="postgres" module="org.postgres">
                    <driver-class>org.postgresql.Driver</driver-class>
                </driver>
            </drivers>
        </datasources>

然后,在我的 Eclipse 项目中,我将 JPA 添加到项目方面,并像这样修改 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="weathermonitor"
    transaction-type="JTA">

    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:jboss/datasources/weathermonitor</jta-data-source>
    <properties>
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
    </properties>

</persistence-unit>

在我的 Java DAO Class 中,我尝试注入实体管理器:

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

但是,我最终得到了 NullPointerException。

谁能告诉我我做错了什么?

由于您没有提到未包含在 WildFly 中的 PostgreSQL JDBC 驱动程序,我怀疑您忘记将其作为模块安装。

我找到了解决方案:我在我的 EJB 中使用 "new"-operator 实例化了 DAO class,而不是使用 @Inject 注释注入它。