Hibernate Tools

Tooling for your Hibernate projects.

Reverse engineer Java entities from an existing SQL database schema.

Reverse Engineering from a Database

Hibernate Tools is a set of build plugins and a Java library that can be used to generate Hibernate/JPA entities starting from your relational database structure. The default behavior of the code generator is highly customizable via XML or by plugging in custom generation strategies. In addition, other artefacts such as DAO classes, HTML documentation, or even UI components can be generated by using and/or adding additional generation templates. The API can be used from custom Java classes but is also readily usable in Maven, Gradle and Ant. Other possibilities such as JBang and Quarkus are in the works.

Reverse Engineering

Project Configuration

For the purpose of a quick introduction, let’s assume that you have a database running, e.g. H2 Sakila database reacheable at the following JDBC URL: jdbc:h2:tcp://localhost/./sakila.

Let’s assume as well that a hibernate.properties file below is available somewhere on the project’s class path:

hibernate.connection.driver_class=org.h2.Driver
hibernate.connection.url=jdbc:h2:tcp://localhost/./sakila
hibernate.connection.username=sa
hibernate.default_catalog=SAKILA
hibernate.default_schema=PUBLIC

With this in place let’s show some possible uses of the reverse engineering tooling to create Java classes from the database.

Using the Maven Plugin

The Hibernate Tools Maven plugin can be used either directly or by configuration in the pom.xml file of your project to generate entity classes or other artefacts. Let’s assume you have created a Maven project with the following pom.xml file.

<?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>org.bar</groupId>
    <artifactId>foo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <hibernate-core.version>${hibernate.version}</hibernate-core.version>
        <h2.version>${h2.version}</h2.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.hibernate.orm</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate-core.version}</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>${h2.version}</version>
        </dependency>
    </dependencies>

</project>

Let’s assume as well that the src/main/resources folder contains the hibernate.properties file shown earlier.

Having this in place simply issuing

mvn org.hibernate.tool:hibernate-tools-maven:${hibernate.version}:hbm2java

from a command line prompt in the project folder would use all the default settings to generate Java classes for all the tables in target/generated-sources/.

me@machine foo % ls target/generated-sources
Actor.java          Film.java               Inventory.java
Address.java        FilmActor.java          Language.java
Category.java       FilmActorId.java        Payment.java
City.java           FilmCategory.java       Rental.java
Country.java        FilmCategoryId.java     Staff.java
Customer.java       FilmText.java           Store.java
me@machine foo %

Of course all these defaults can be overridden and tuned. Please consult the documentation for more information.

Using Gradle Tasks

Let’s assume in this case that we start off with a very simple default Gradle application, e.g. created with gradle init --type java-application --dsl groovy.

With this setup, we replace the contents of the file app/build.gradle with the code below :

plugins {
    id('application')
    id('org.hibernate.tool.hibernate-tools-gradle') version '${hibernate-tools.version}'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation('com.h2database:h2:${h2.version}')
}

As a last precondition, we make sure that the above hibernate.properties file is present in folder app/src/main/resources.

With this in place, issuing

gradle generateJava

will use all the default settings to generate Java classes for all the tables in app/generated-sources/.

Using Good Old Ant

Creating an Ant project is very simple. We only need a build.xml file to be present in the root of the project. In order to manage the needed dependencies, the easiest way is to use the Ant tasks provided by Apache Ivy.

Our build.xml file will be looking like below:

<project xmlns:ivy="antlib:org.apache.ivy.ant">

    <property name="hibernate.tools.version" value=the-hibernate-tools-version-to-use/>
    <property name="h2.version" value=the-h2-version-to-use/>

    <ivy:cachepath organisation="org.hibernate.tool" module="hibernate-tools-ant" revision="${hibernate.tools.version}"
                   pathid="hibernate-tools" inline="true"/>
    <ivy:cachepath organisation="com.h2database" module="h2" revision="${h2.version}"
                   pathid="h2" inline="true"/>

    <path id="classpath">
        <path refid="hibernate-tools"/>
        <path refid="h2"/>
    </path>

    <taskdef name="hibernatetool"
             classname="org.hibernate.tool.ant.HibernateToolTask"
             classpathref="classpath" />

    <target name="reveng">
        <hibernatetool destdir="generated-sources">
            <jdbcconfiguration propertyfile="hibernate.properties" />
            <hbm2java/>
        </hibernatetool>
    </target>

</project>

With the hibernate.properties file from above also present in the root of the project, simply issuing

ant reveng

would generate the Java files in the folder generated-sources.

Back to top