【问题标题】:postgres database with wildfly in eclipseeclipse中带有wildfly的postgres数据库
【发布时间】:2015-05-14 06:03:41
【问题描述】:

我在使用 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 类中,我尝试注入实体管理器:

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

但是,我最终得到了 NullPointerException。

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

【问题讨论】:

    标签: hibernate postgresql jakarta-ee jpa wildfly


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2016-07-02
        • 2017-10-03
        • 2016-01-28
        • 1970-01-01
        • 2012-10-31
        • 2021-12-17
        • 2017-04-04
        • 2017-01-09
        • 1970-01-01
        相关资源
        最近更新 更多