使用 Gradle 进行 Hibernate Tools 逆向工程

问题描述 投票:0回答:2

当我使用 Eclipse 作为 IDE 时,我曾经使用 Hibernate Tools 对数据库进行逆向工程以获得我的实体,并带有注释。

我最近转向了 IntelliJ IDEA,我认为它是一个总体更好的 IDE,但不幸的是没有适用于它的 Hibernate Tools 端口,因此我无法像以前那样生成实体。我知道 IntelliJ IDEA 有自己的逆向工程工具(通过

Persistence->Generate Persistence Mapping->By Database Schema
访问的工具),但我发现它有点错误,有时会生成完全错误的实体。

我知道 Hibernate Tools 也可以从 Ant 使用。有没有办法从 Gradle 中使用它?

java hibernate gradle ant hibernate-tools
2个回答
1
投票

我成功地使用了 Gradle 中的 Hibernate Tools,很大程度上要归功于 这个问题

事实证明(我不知道)Gradle 确实能够调用 Ant 任务,因此可以使用预先存在的 Hibernate Tools Ant 任务来对数据库进行逆向工程。

为此,需要有一个

hibernate.cfg.xml
文件,其中包含告诉 Ant 询问如何访问我们的数据库所需的配置。这是一个例子:

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
        "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/mydb
        </property>
        <property name="hibernate.connection.username">
            username
        </property>
        <property name="hibernate.connection.password">
            password
        </property>
    </session-factory>
</hibernate-configuration>

(IntelliJ 可能会抱怨这个文件,告诉它找不到驱动程序,但这没关系,因为它们将在执行过程中由 Gradle 提供)

此配置文件将由 Gradle 调用的 Ant 任务使用。我将其放入在项目根目录中创建的新

db
文件夹中。

需要将以下内容添加到

build.gradle
文件中:

configurations {
    reverseMap
}

dependencies {
    //...your other dependencies...
    reverseMap 'org.hibernate:hibernate-core:4.0.1.Final'
    reverseMap 'org.hibernate:hibernate-tools:4.0.1.Final'
    reverseMap 'org.slf4j:slf4j-simple:1.7.5'
    reverseMap 'mysql:mysql-connector-java:5.1.48'
}

project.ext {
    hibernateDestDir = file("$projectDir/src/main/java")
}

task reverseMap {
    outputs.dir hibernateDestDir
    doLast {
        hibernateDestDir.exists() || hibernateDestDir.mkdirs()
        ant {
            taskdef(
                name: 'hibernatetool',
                classname: 'org.hibernate.tool.ant.HibernateToolTask',
                classpath: configurations.reverseMap.asPath
            )
            hibernatetool(destdir: hibernateDestDir) {
                jdbcconfiguration(
                    configurationfile: "$projectDir/db/hibernate.cfg.xml",
                    packagename: "com.me.models"
                )
                hbm2java(
                    jdk5: true,
                    ejb3: true
                )
            }
        }
    }
}

此代码创建一个名为

reverseMap
的新配置,可用于声明
reverseMap
任务所需的依赖项(需要
hibernate-core
hibernate-tools
log4j
,而驱动程序应该是所需的依赖项)对于您的 DBMS)。

reverseMap
代码调用Ant任务,基本上遵循官方指南。感兴趣的部分是
hbm2java
,它是实际的出口商。其余代码基本上是 Ant 任务和配置的粘合代码。

Gradle 任务可以从命令行 (

./gradlew reverseMap
) 或 IntelliJ 调用。


0
投票

您使用的 Java 版本是什么? Mine Gradle 8.10.1 在 Java 17 上尝试从 FireBird 数据库导出实体时出现以下错误:

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':reverseMap'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.lambda$executeIfValid$1(ExecuteActionsTaskExecuter.java:130)
    at org.gradle.internal.Try$Failure.ifSuccessfulOrElse(Try.java:293)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeIfValid(ExecuteActionsTaskExecuter.java:128)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:116)
    at org.gradle.api.internal.tasks.execution.FinalizePropertiesTaskExecuter.execute(FinalizePropertiesTaskExecuter.java:46)
    at org.gradle.api.internal.tasks.execution.ResolveTaskExecutionModeExecuter.execute(ResolveTaskExecutionModeExecuter.java:51)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:57)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:74)
    at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:36)
    at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.executeTask(EventFiringTaskExecuter.java:77)
    at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.call(EventFiringTaskExecuter.java:55)
    at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.call(EventFiringTaskExecuter.java:52)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:209)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:204)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:66)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:59)
    at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:166)
    at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:59)
    at org.gradle.internal.operations.DefaultBuildOperationRunner.call(DefaultBuildOperationRunner.java:53)
    at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter.execute(EventFiringTaskExecuter.java:52)
    at org.gradle.execution.plan.LocalTaskNodeExecutor.execute(LocalTaskNodeExecutor.java:42)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:331)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:318)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.lambda$execute$0(DefaultTaskExecutionGraph.java:314)
    at org.gradle.internal.operations.CurrentBuildOperationRef.with(CurrentBuildOperationRef.java:85)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:314)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:303)
    at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.execute(DefaultPlanExecutor.java:459)
    at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.run(DefaultPlanExecutor.java:376)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
    at org.gradle.internal.concurrent.AbstractManagedExecutor$1.run(AbstractManagedExecutor.java:48)

引起::java.lang.NullPointerException:无法调用“java.util.Properties.put(Object, Object)”,因为“properties”为空

© www.soinside.com 2019 - 2024. All rights reserved.