Movatterモバイル変換


[0]ホーム

URL:


ORM
AboutReleasesDocumentationQuicklyMigration guidesBooksProcessorToolingEnversContributePaid support
Source codeIssue trackerSecurity issueForumCIRoadmap
Search
AboutReleasesDocumentationMigration guidesMore resourcesRoadmapContributeToolingPaid supportFAQ
Source codeIssue trackerSecurity issueForumWikiCI
Validator
AboutReleasesDocumentationMigration guidesRoadmapContributeToolingPaid supportFAQ
Source codeIssue trackerSecurity issueForumCI
Reactive
AboutReleasesDocumentationContributePaid support
Source codeIssue trackerSecurity issueForumCI
Repositories
AboutReleasesDocumentationQuickly
Source codeIssue trackerSecurity issue
Tools
AboutDocumentationContribute
Source codeIssue trackerSecurity issueForumCI
Others
Community
CommunityGovernanceCommonhaus - MembershipCommonhaus - Joining FAQContribute - GuidelinesContribute - Set up IntelliJ IDEAContribute - Set up Eclipse IDEContribute - Build Hibernate ORMTeamContributors to ORMContributors to SearchContributors to ValidatorContributors to ReactiveContributors to ToolsContributors to OGMCorporate contributorsCompatibility policyMaintenance policyLicensesKeys
BlogForumsFollow us
HibernateMenu
Hibernate
ORMSearchValidatorReactiveRepositoriesToolsOthers
BlogForumsCommunityFollow us

Hibernate Tools

Tooling for your Hibernate projects.

Reverse engineer Java entities from an existing SQL database schema.

AboutDocumentationContribute
Source codeIssue trackerSecurity issueForumCI

Released under theLGPL v2.1

Reverse engineering from a database

Hibernate Tools generates entity classes from a relational database schema.The code generator is is written in Java, and can be called directly from Java code, but it’s also integrated withMaven, Gradle, and Ant.

Reverse Engineering

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 generatedvia the use of custom or built-in generation templates.

Project configuration

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.

Using the Maven plugin

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:hbm2java

from 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.

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 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 generateJava

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

Using good old Ant

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 reveng

generates the Java files in the foldergenerated-sources.

Latest news

Other news

Projects

Follow us

Contribute and community

Back to top

[8]ページ先頭

©2009-2025 Movatter.jp