Custom Hibernate ORM and Hibernate Search on WildFly Swarm

This article will show how to use a custom version of Hibernate (Hibernate ORM and Hibernate Search) on a WildFly Swarm Java application. I won’t give details about the WildFly Swarm configuration, if you need more information you can take a look at the WildFly Swarm User’s Guide.

WildFly Swarm includes fractions for Hibernate ORM and Hibernate Search.
You can add those fractions using:

<dependency>
    <groupId>org.wildfly.swarm</groupId>
    <artifactId>jpa</artifactId>
</dependency>
<dependency>
 <groupId>org.wildfly.swarm</groupId>
 <artifactId>hibernate-search</artifactId>
</dependency>

Unfortunately, WildFly Swarm doesn’t use the latest versions of Hibernate ORM and Hibernate Search, so you won’t be able to use some functionalities of Hibernate that are just available on latest versions. For instance, the latest version of Hibernate Search offers the capability to integrate Elasticsearch. For more information go to the Hibernate Search documentation.

Solution

  • Create a WildFly Swarm application. You can use the WildFly Swarm generator, JBoss Forge, or choose another option.
  • Add WildFly Swarm fractions (Hibernate Search and ORM) to your project:
<dependency>
    <groupId>org.wildfly.swarm</groupId>
    <artifactId>jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.wildfly.swarm</groupId>
    <artifactId>datasources</artifactId>
</dependency>
<dependency>
    <groupId>org.wildfly.swarm</groupId>
    <artifactId>hibernate-search</artifactId>
</dependency>
<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>unpack</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-orm-modules</artifactId>
                        <version>5.2.12.Final</version>
                        <classifier>wildfly-11-dist</classifier>
                        <type>zip</type>
                        <overWrite>false</overWrite>
                        <outputDirectory>${project.build.outputDirectory}/modules</outputDirectory>
                    </artifactItem>
                    <artifactItem>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-search-modules</artifactId>
                        <version>5.8.2.Final</version>
                        <classifier>wildfly-11-dist</classifier>
                        <type>zip</type>
                        <overWrite>false</overWrite>
                        <outputDirectory>${project.build.outputDirectory}/modules</outputDirectory>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

This will add Hibernate modules to the folder /modules of your project (WildFly Swarm will detect all modules under the /modules folder). For more information about how to use Hibernate modules, take a look at Hibernate ORM at WildFly and Hibernate Search at WildFly

  • Add persistence.xml to your project and then add Hibernate properties:
<property name="jboss.as.jpa.providerModule" value="org.hibernate:5.2.12.Final"/>

<property name="wildfly.jpa.hibernate.search.module" value="org.hibernate.search.orm:5.8.2.Final"/>

 

That’s all, now you have you own version of Hibernate on your WildFly Swarm Application. If you want to see the code, here is a repo with a complete example: https://github.com/carlosthe19916/wildfly-swarm-hibernate

Everything you need to grow your career.

With your free Red Hat Developer program membership, unlock our library of cheat sheets and ebooks on next-generation application development.

SIGN UP

Warning:

The example runs on any version of WildFly Swarm Application except for 2017.12.0, because of recent bugs resolved on 2018.1.0.

If you have any questions about this post, please let me know!!

Share