The IntelliJ IDEA Blog

Reverse Engineering with Hibernate 7.4 and IntelliJ IDEA

8.5内容质量

TL;DR · AI 摘要

Hibernate 7.4与IntelliJ IDEA结合实现数据库反向工程,提升Java持久化开发效率。

核心要点

  • Hibernate 7.4通过Maven插件支持反向工程,生成JPA实体类和映射文件
  • IntelliJ IDEA可生成Flyway/Liquibase迁移脚本,提升数据库版本管理效率
  • 配置文件支持自定义类型映射和表过滤规则,适配复杂数据库结构

结构提纲

按章节快速跳转。

  1. 定义数据库反向工程在应用开发中的价值和使用场景

  2. ·Hibernate 7.4反向工程

    介绍Maven插件配置和hibernate-reverse-engineering.xml文件的使用方法

  3. 展示类型映射、表过滤和主键生成等配置项的具体实现

  4. ·IntelliJ IDEA反向工程

    说明IDE生成实体类和迁移脚本的功能及操作流程

  5. 比较Hibernate工具和IDEA反向工程功能的异同及适用场景

  6. 强调自动化反向工程对提升开发效率和减少错误的重要作用

思维导图

用一张图看清主题之间的关系。

查看大纲文本(无障碍 / 无 JS 友好)
  • Hibernate 7.4与IntelliJ IDEA反向工程
    • Hibernate 7.4功能
      • Maven插件配置
      • 类型映射自定义
      • 表过滤规则
    • IntelliJ IDEA功能
      • 实体类生成
      • 迁移脚本生成
    • 使用场景
      • 遗留系统改造
      • 数据库优先开发

金句 / Highlights

值得收藏与分享的关键句。

#Hibernate#Java#IntelliJ IDEA#反向工程#JPA
打开原文

Reverse Engineering with Hibernate 7.4 and IntelliJ IDEA - The JetBrains Blog

IntelliJ IDEA

IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin

Follow

  • Follow:
  • Linkedin Linkedin
  • Bluesky Bluesky
  • X X
  • Facebook Facebook
  • Youtube Youtube
  • RSS RSS

Download

IntelliJ IDEA

Java

Reverse Engineering with Hibernate 7.4 and IntelliJ IDEA

Siva Katamreddy

Reverse Engineering in the context of database-driven application development means generating Java persistence artifacts such as entity classes and mapping files from an existing database schema.

This is useful when the database already exists, especially in legacy systems or large projects where creating entity classes manually can be slow and error-prone.

Until now, these features were provided by the separate Hibernate Tools project. Starting with Hibernate 7.4, the core Hibernate Tools modules are part of the Hibernate repository . Maven and Gradle (and even Ant) users can now access reverse engineering through the Hibernate build plugins. This is especially useful for database-first projects, where the schema already exists and the application needs an initial set of persistence artifacts.

IntelliJ IDEA also provides Reverse Engineering features that can help developers generate JPA entities from an existing database, generate Flyway or Liquibase migrations from JPA/JDBC entities, and much more.

In this article, let us explore both Hibernate 7.4 and IntelliJ IDEA Reverse Engineering capabilities and understand which tool to use in which scenario.

You can check out the sample code for this article in this GitHub repository .

Hibernate 7.4 Reverse Engineering

Hibernate 7.4 can inspect an existing database and generate artifacts such as:

  • JPA entity classes
  • Hibernate mapping files
  • DAO-style helper classes
  • Database schema SQL scripts

In the sample project , the reverse engineering workflow is configured using the Maven plugin with reverse engineering customization specified in hibernate-reverse-engineering.xml as follows:

code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering SYSTEM
        "https://hibernate.org/dtd/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
    <type-mapping>
        <sql-type jdbc-type="NUMERIC" hibernate-type="big_decimal"/>
        <sql-type jdbc-type="OTHER" hibernate-type="pg-uuid"/>
    </type-mapping>

    <table-filter match-name=".*" package="com.jetbrains.entities"/>
    <table-filter match-name="flyway.*" exclude="true"/>

    <table name="products" class="Product">
        <primary-key property="id">
            <generator class="sequence">
                <param name="sequence_name">product_id_seq</param>
            </generator>
        </primary-key>
        <!-- other config -->
    </table>

    <!-- other tables config -->
</hibernate-reverse-engineering>

This configuration lets us customize type mappings, package names, table filters, class names, and primary key generation.

Configure the hibernate-maven-plugin in pom.xml as follows:

code
<properties>
   <!-- update the version to the newer version of Hibernate -->
   <hibernate.version>7.4.0.CR1</hibernate.version> 
</properties>

<plugin>
    <groupId>org.hibernate.orm</groupId>
    <artifactId>hibernate-maven-plugin</artifactId>
    <version>${hibernate.version}</version>
    <configuration>
        <revengFile>hibernate-reverse-engineering.xml</revengFile>
    </configuration>
    <executions>
        <execution>
            <id>generate-entities</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>hbm2ddl</goal>
                <goal>hbm2java</goal>
                <goal>hbm2dao</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The hbm2java goal generates JPA entity classes from database metadata, while hbm2ddl can generate database schema SQL. The hbm2dao goal can generate DAO-style helper classes with methods to perform CRUD operations on entities.

To generate the artifacts, run:

code
$ ./mvnw generate-sources

This Maven goal generates JPA entities and DAO classes in target/generated-sources, and schema.ddl file in target/generated-resources directories.

Regeneration and Version-Controlled Workflows

Generated code is rarely the final version of an application model. After the first generation, developers often add validation annotations, helper methods, domain logic, fetch strategies, and other project-specific changes.

If these files are regenerated later, manual changes can be overwritten. This is why a regeneration workflow may not be ideal when developers edit generated entities directly.

However, regeneration is not always a drawback. Some teams prefer to keep the reverse engineering configuration in source control and make generation part of the build. In this setup, the output is reproducible, works in CI/CD, and does not depend on a specific IDE.

For example, a CI check can regenerate the artifacts and fail if the generated files are different from the committed version.

This workflow also works well with versioned database migrations. Migration scripts can remain the source of truth for schema history, while Hibernate reverse engineering generates Java artifacts from the current database schema. Hibernate also has a built-in schema update mechanism, and the reverse-engineered output can be further customized with custom RevengineeringStrategy , so the underlying model doesn’t have to be a rigid, one-size-fits-all script either.

IntelliJ IDEA Reverse Engineering

IntelliJ IDEA provides a more interactive and flexible reverse engineering workflow. Instead of creating and maintaining a reverse engineering XML file, we can connect to a database from the IDE, select the tables, and generate JPA entities directly.

More importantly, IntelliJ IDEA supports progressive synchronization between the database schema and the code. When the database changes, the IDE can help sync those changes into existing entity classes without forcing a full regeneration. This helps preserve manual modifications that were made after the initial generation.

That difference matters in real projects. Entity classes often become part of the domain model, not just generated database mirrors. Being able to evolve generated entities safely is more valuable than repeatedly recreating them. This functionality is provided by the IntelliJ IDEA Ultimate, so you need a subscription. It is not available in IntelliJ IDEA Community Edition or in other IDEs (yet), and it cannot be run as a command-line step in a build pipeline.

More Feature-Rich and Flexible

IntelliJ IDEA’s reverse engineering provide additional features apart from generating JPA entities from tables. The IDE supports related workflows such as:

  • Generating JPA entities from an existing database schema
  • Synchronizing database changes into existing entity classes
  • Preserving manual changes during incremental updates
  • Generating Flyway migrations from JPA entities
  • Generating Liquibase migrations from JPA entities
  • Supports Spring Data JDBC entities in addition to JPA

To learn more about these features, explore the following articles:

  • How to Use Flyway for Database Migrations in Spring Boot Applications
  • Spring Data JDBC Made Easy with IntelliJ IDEA

This makes IntelliJ IDEA a better fit for day-to-day development, especially when the schema and code evolve together. It provides a guided workflow, visual database integration, and incremental updates without requiring a build-time generation configuration for every change.

Choosing the Right Workflow

Hibernate 7.4 reverse engineering is a strong option when we want a repeatable, build-driven process. It works well for generating the initial version of entities and schema SQL from an existing database, especially in automated workflows.

IntelliJ IDEA is more suitable when we want an iterative workflow. It helps developers progressively synchronize code and database changes without losing manual entity customizations. It also supports additional workflows around migrations and Spring Data JDBC, making it more feature-rich and flexible for application development.

Summary

Both approaches solve the same core problem of generating Java persistence artifacts from an existing database schema, but they are designed for different situations.

Use Hibernate 7.4 Reverse Engineering when:

  • You want the generation to be part of the build process to generate entities, DAOs, .hbm files based on the current database schema.
  • You need this to work in any IDE, or from the command line, or in a CI/CD pipeline, since it runs through Gradle or Maven.
  • You are comfortable regenerating entities, DAOs, and .hbm files from a version-controlled reveng.xml file rather than editing generated code by hand.

Use IntelliJ IDEA Reverse Engineering when:

  • You want an interactive and visual way to generate entities without writing any XML configuration.
  • Your database schema changes over time, and you need to keep the entity classes in sync without losing the custom logic you have already added.
  • You are working with Spring Data JDBC in addition to JPA, or you need to generate Flyway or Liquibase migration scripts.
  • You have an IntelliJ IDEA Ultimate subscription and don’t need reverse engineering to run outside the IDE (for example, in a CI/CD pipeline)

IntelliJ IDEA is generally a strong choice for day-to-day development because it lets you evolve the database and the Java model together, as long as you have an Ultimate subscription and don’t need the step to run outside the IDE. It understands what already exists in your code and can merge schema changes into it, rather than replacing everything from scratch.

If your team needs this to run in any IDE or as part of a CI/CD pipeline, or if you prefer a version-controlled, reproducible generation step, Hibernate 7.4’s build-tool-based reverse engineering is the better fit.

If you are starting a brand-new project from an existing database, either approach can give you a solid starting point. From there, the right choice mostly comes down to your team’s workflow and tooling constraints.

Hibernate

JPA

Reverse Engineering

  • Share
  • Facebook
  • Twitter
  • Linkedin

Prev post

What’s New in IntelliJ IDEA 2026.2