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 generatedvia the use of custom or built-in generation templates.
Let’s suppose that we have a database running, for example, theH2 Sakila database.
Let’s also suppose that the followinghibernate.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 thedatabase.
The Hibernate Tools Maven plugin can be used either directly or by configuration in thepom.xml file of ourproject to generate entity classes or other artefacts.Let’s assume we have created a Maven project with the followingpom.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<projectxmlns="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>7.1.10.Final</hibernate-core.version>
<h2.version>2.3.232</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 thesrc/main/resources folder contains thehibernate.properties file shown earlier.
Having this in place, simply issuing:
mvn org.hibernate.tool:hibernate-tools-maven:7.1.10.Final:hbm2javafrom a command line prompt in the project folder will use all the default settings to generate Java classes for thetables intarget/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. Refer to thedocumentation for more information.
Let’s assume in this case that we start off with a very simple default Gradle application, for example, a project created viagradle init --type java-application --dsl groovy.
With this setup, we replace the contents of the fileapp/build.gradle with the code below:
plugins {
id('application')
id('org.hibernate.tool.hibernate-tools-gradle') version'7.1.10.Final'
}
repositories {
mavenCentral()
}
dependencies {
implementation('com.h2database:h2:2.3.232')
}As a final precondition, we make sure that the abovehibernate.properties file is present in folderapp/src/main/resources.
With this in place, issuing:
gradle generateJavawill use all the default settings to generate Java classes for the tables inapp/generated-sources/.
Creating an Ant project is very simple. We only need abuild.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.
Ourbuild.xml file will be looking like below:
<projectxmlns:ivy="antlib:org.apache.ivy.ant">
<propertyname="hibernate.tools.version"value=the-hibernate-tools-version-to-use/>
<propertyname="h2.version"value=the-h2-version-to-use/>
<ivy:cachepathorganisation="org.hibernate.tool"module="hibernate-tools-ant"revision="7.1.10.Final"
pathid="hibernate-tools"inline="true"/>
<ivy:cachepathorganisation="com.h2database"module="h2"revision="2.3.232"
pathid="h2"inline="true"/>
<pathid="classpath">
<pathrefid="hibernate-tools"/>
<pathrefid="h2"/>
</path>
<taskdefname="hibernatetool"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="classpath"/>
<targetname="reveng">
<hibernatetooldestdir="generated-sources">
<jdbcconfigurationpropertyfile="hibernate.properties"/>
<hbm2java/>
</hibernatetool>
</target>
</project>With thehibernate.properties file from above also present in the root of the project, simply issuing:
ant revenggenerates the Java files in the foldergenerated-sources.