如何使用Hibernate OGM加入MongoDB和Wildfly

时间:2016-12-04 21:06:22

标签: hibernate wildfly hibernate-ogm

我尝试通过JavaEE7(JAX-RS),Hibernate OGM,MongoDB和Wildfly 10构建REST应用程序。我试图引用THIS教程。 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>

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

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

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

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

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

Hibernate OGM模块应位于:

$ WILDFLY_HOME /模块/组织/休眠/ OGM /

如果缺少它们,只需从modules文件夹中的SourceForge中提取ZIP。