如何使用 Hibernate OGM 加入 MongoDB 和 Wildfly

How to join MongoDB and Wildfly using Hibernate OGM

我正在尝试通过 JavaEE7 (JAX-RS)、Hibernate OGM、MongoDB 和 Wildfly 10 构建 REST 应用程序。我试图参考 THIS tutorial. HERE 你可以还可以找到教程的完整源代码。我在当前版本的 maven 文件中替换了旧版本的依赖项。目前,当我尝试将我的应用程序部署到 Wildfly 时,出现以下错误:

21:20:25,723 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-6) MSC000001: Failed to start service jboss.module.service."deployment.RESTApp.war".main: org.jboss.msc.service.StartException in service jboss.module.service."deployment.RESTApp.war".main: WFLYSRV0179: Failed to load module: deployment.RESTApp.war:main
    at org.jboss.as.server.moduleservice.ModuleLoadService.start(ModuleLoadService.java:91)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
> 
> Caused by: org.jboss.modules.ModuleNotFoundException: org.hibernate:ogm
    at org.jboss.modules.Module.addPaths(Module.java:1092)
    at org.jboss.modules.Module.link(Module.java:1448)
    at org.jboss.modules.Module.relinkIfNecessary(Module.java:1476)
    at org.jboss.modules.ModuleLoader.loadModule(ModuleLoader.java:225)
    at org.jboss.as.server.moduleservice.ModuleLoadService.start(ModuleLoadService.java:68)
    ... 5 more

21:20:32,849 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "RESTApp.war")]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.module.service.\"deployment.RESTApp.war\".main" => "org.jboss.msc.service.StartException in service jboss.module.service.\"deployment.RESTApp.war\".main: WFLYSRV0179: Failed to load module: deployment.RESTApp.war:main
    Caused by: org.jboss.modules.ModuleNotFoundException: org.hibernate:ogm"}}

问题:我该如何解决这个问题?

您可以在下面找到 pom.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>

<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>RESTApp</groupId>
    <artifactId>RESTApp</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>RESTApp</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>

        <version.org.hibernate.ogm>5.0.3.Final</version.org.hibernate.ogm>
        <version.org.wildfly>10.1.0.Final</version.org.wildfly>
        <version.org.jboss.arquillian>1.1.11.Final</version.org.jboss.arquillian>
        <version.org.wildfly.arquillian>2.0.0.Final</version.org.wildfly.arquillian>

        <jboss.home>C:\MySpace\WildFly\Wildfly_10\WILDFLY_HOME</jboss.home>
        <ogm.module.path>C:\MySpace\Eclipse\eclipseEE\workspace\RESTApp\target\hibernate-ogm-modules</ogm.module.path>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.hibernate.ogm</groupId>
                <artifactId>hibernate-ogm-core</artifactId>
                <type>pom</type>
                <version>${version.org.hibernate.ogm}</version>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.jboss.arquillian</groupId>
                <artifactId>arquillian-bom</artifactId>
                <version>${version.org.jboss.arquillian}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>   

    <build>
        <testResources>
            <testResource>
                <directory>src/test/java</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>
        <pluginManagement>
            <plugins>
                <!--
                    May be used to run the application for manual testing, e.g. via curl;
                    Run "mvn pre-integration-test" once to prepare the server and modules, then the server can be
                    started via "mvn wildfly:run"
                -->
                <plugin>
                    <groupId>org.wildfly.plugins</groupId>
                    <artifactId>wildfly-maven-plugin</artifactId>
                    <version>1.1.0.Beta1</version>
                    <configuration>
                        <jboss-home>${jboss.home}</jboss-home>
                        <modules-path>${ogm.module.path}</modules-path>
                        <!--
                         <javaOpts>
                             <javaOpt>-Xdebug</javaOpt>
                             <javaOpt>-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5000</javaOpt>
                         </javaOpts>
                          -->
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.0.0</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.wildfly</groupId>
                                    <artifactId>wildfly-dist</artifactId>
                                    <version>${version.org.wildfly}</version>
                                    <type>zip</type>
                                    <overWrite>false</overWrite>
                                    <outputDirectory>${project.build.directory}</outputDirectory>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>org.hibernate.ogm</groupId>
                                    <artifactId>hibernate-ogm-modules-wildfly10</artifactId>
                                    <version>${version.org.hibernate.ogm}</version>
                                    <type>zip</type>
                                    <overWrite>false</overWrite>
                                    <outputDirectory>${ogm.module.path}</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.19.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- <artifactId>hibernate-ogm-infinispan</artifactId>  -->
        <dependency>
            <groupId>org.hibernate.ogm</groupId>            
            <artifactId>hibernate-ogm-mongodb</artifactId>            
            <scope>provided</scope>
        </dependency>       

        <!-- Only needed at build time for generating a mapper implementation -->
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>1.1.0.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>1.1.0.Final</version>
            <scope>provided</scope>
        </dependency>

        <!-- Testing -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.skyscreamer</groupId>
            <artifactId>jsonassert</artifactId>
            <version>1.4.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.junit</groupId>
            <artifactId>arquillian-junit-container</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.wildfly.arquillian</groupId>
            <artifactId>wildfly-arquillian-container-managed</artifactId>
            <version>${version.org.wildfly.arquillian}</version>
            <scope>test</scope>
            <exclusions>
                <!-- This exclusion is needed to be able to setup the project in Windows:
                     it otherwise includes transitive dependency to the JDK JConsole -->
                <exclusion>
                    <artifactId>wildfly-patching</artifactId>
                    <groupId>org.wildfly</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.extension</groupId>
            <artifactId>arquillian-rest-client-api</artifactId>
            <version>1.0.0.Alpha4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.extension</groupId>
            <artifactId>arquillian-rest-client-impl-3x</artifactId>
            <version>1.0.0.Alpha4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

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

        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>

        <class>com.restapp.model.Subscriber</class>

        <properties>

            <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform" />

            <property name="hibernate.ogm.datastore.provider" value="mongodb" />
            <property name="hibernate.ogm.datastore.database" value="db_name" />
            <property name="hibernate.ogm.datastore.host" value="127.0.0.1" />
            <property name="hibernate.ogm.datastore.port" value="27017" />
            <property name="hibernate.ogm.datastore.username" value="robert" />
            <property name="hibernate.ogm.datastore.password" value="pwd" />
        </properties>

    </persistence-unit>   
</persistence>

也许 jboss-deployment-structure.xml 文件也有用:

<?xml version="1.0" encoding="UTF-8"?>
<!--
 ~ Hibernate OGM, Domain model persistence for NoSQL datastores
 ~
 ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later
 ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<jboss-deployment-structure
    xmlns="urn:jboss:deployment-structure:1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <deployment>
        <dependencies>
            <module name="org.hibernate" slot="ogm" services="import" />
            <module name="org.hibernate.ogm.mongodb" services="import" />
            <module name="org.hibernate.search.orm" services="import" />
        </dependencies>
    </deployment>
</jboss-deployment-structure>

WildFly 找不到 Hibernate OGM 模块。 你添加了这个模块吗? 您没有写任何关于如何添加缺少的模块及其配置的内容,因此很难猜测为什么 Wildfly 找不到它。

在最新版本中,Hibernate OGM 更改了 WildFly 中模块的名称,因此您需要更新您的 jboss-deployment-structure.xml。 新位置是:

<module name="org.hibernate.ogm" slot="main" services="import" />

或者,如果您需要不同的版本系列,您可以 select 不同的插槽。例如,对于 5.1 将是:

<module name="org.hibernate.ogm" slot="5.1" services="import" />

您需要检查您需要的模块是否存在于 WildFly 10 模块文件夹中(you can download them from SourceForge 如果它们不存在)。

编辑:我没有添加模块所在的位置。

Hibernate OGM 模块应位于:

$WILDFLY_HOME/modules/org/hibernate/ogm/

如果它们丢失,只需从模块文件夹中的 SourceForge 中提取 ZIP。