首页 > 解决方案 > 如何通过 Hibernate 逆向工程使用 Hibernate 工具为“Single DB”生成模型代码

问题描述

我通过JBoss Tools > Hibernate Tools使用Hibernate 逆向工程来生成模型类。

在那里,当我添加 Hibernate 配置并运行它/重建它时,它会列出所有 DB,尽管我在 hibernate.cfg.xml 的 hibernate.connection.url 中只提到了一个 DB。例如:jdbc:mysql://localhost:3306/booksdb。

休眠.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/booksdb</property>
        <property name="hibernate.connection.username">xxxx</property>
        <property name="hibernate.connection.password">xxxx</property>
    </session-factory>
</hibernate-configuration>

在此处输入图像描述

然后,当我生成实体代码时,它还会扫描所有其他数据库表以生成所有实体代码。有时它会受到不同数据库中的一些重复表的影响。

问题: 如何对 hibernate.connection.url 中提到的单个数据库执行此操作?

Hibernate 代码生成配置 > 主要 在此处输入图像描述

Hibernate 代码生成配置 > 导出器 在此处输入图像描述

休眠版本:5.4

MySQL 版本信息

标签: javamysqlhibernatehibernate-tools

解决方案


在此屏幕中,转到附近的设置reveng.xml。它会要求您创建一个 xml,从那里您将能够排除您不想要的数据库。

在这个画面中

<hibernate-reverse-engineering>
    <table-filter match-name=".*" exclude="true" match-catalog="YOUR_DATABASE_NAME" />
</hibernate-reverse-engineering>

推荐阅读