JPA、Wildfly 14 和 PostgreSQL

JPA, Wildfly 14 and PostgreSQL

我正在尝试使用 JPA 和 wildfly 14 作为网络服务器连接到我的 PostgreSQL 数据库。我遵循了几个教程,但我总是得到:

Can't find a persistence unit named 'test'

我做了什么:

下载 postgresql-42.2.5.jar 并复制到 "wildfly-14.0.1.Final\modules\system\layers\base\org\postgres\main\" module.xml 在同一个目录中

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.3" name="org.postgresql">
    <resources>
        <resource-root path="postgresql-42.2.5.jar"/>
        <!-- Make sure this matches the name of the JAR you are installing -->
    </resources>
    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
    </dependencies>
</module>

然后修改了standallone-full.xml

<datasource jta="true" jndi-name="java:jboss/datasources/test" pool-name="test" enabled="true" use-ccm="true">
  <connection-url>jdbc:postgresql://localhost:5432/test</connection-url>
  <driver-class>org.postgresql.Driver</driver-class>
  <driver>postgres</driver>
  <security>
      <user-name>postgres</user-name>
      <password>8a6rbd7a5c3a3fsavf4rq</password>
  </security>

<driver name="postgres" module="org.postgres">
    <driver-class>org.postgresql.Driver</driver-class>
</driver>

现在 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:jboss/datasources/test</jta-data-source>
            <properties>
                <property name="hibernate.hbm2ddl.auto" value="update" />
                <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            </properties>    
        </persistence-unit>
</persistence>

之后,当然还有一点java:

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

所以,这就是我所做的一切 - 但尝试了更多...... 在部署中出现了错误:

WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'test' in deployment ...

我不知道该怎么办。我是否忘记了一些 Maven 依赖项?

请帮忙。

module.xml 中的 name 必须匹配路径。 (没有主要)

改为<module xmlns="urn:jboss:module:1.3" name="org.postgres">

编辑:

此外,推荐的放置模块的位置是直接放入 $JBOSS_HOME\modules

所以在你的情况下:$JBOSS_HOME\modules\org\postgres\main\