Customizable code generation
The generated code is highly customizable:
-
via XML-based configuration, or
-
by plugging in custom generation strategies.
In addition to entities, artefacts such as DAO classes, HTML documentation, or even UI components may be generated via the use of custom or built-in generation templates.
Project configuration
Let’s suppose that we have a database running, for example, the H2 Sakila database.
Let’s also suppose that the following hibernate.properties
file is available somewhere on the project 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 our
project to generate entity classes or other artefacts.
Let’s assume we 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 also assume 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 will use all the default settings to generate Java classes for 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, for example, a project
created via 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 final 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 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
generates the Java files in the folder generated-sources
.