休眠:Gradle Ant 找不到 hibernate.reveng.xml

Hibernate: Gradle Ant cannot find hibernate.reveng.xml

我正在创建一个从数据库生成 java 类 的任务。我使用 ant 创建了一个 gradle 任务。奇怪的是,该任务需要一个 hibernate.cfg.xml 文件 revengfile 属性。

这是我的 gradle 任务:

task hibernate{
   description "Generates Java Classes from the Database Schema."
   group "Hibernate"

   doLast{
    ant {
        def base = "$projectDir/src/main/java"

        taskdef(name: 'hibernatetool', 
            classname: 'org.hibernate.tool.ant.HibernateToolTask', 
            classpath: configurations.compile.asPath )

        hibernatetool( destdir : "$base" ) { 

            jdbcconfiguration(
                propertyfile:"src/main/resources/hibernate.properties", revengfile:"src/main/resources/hibernate.reveng.xml", //expects hibernate.cfg.xml instead
                packagename: springProperties["hibernate.generate.package"], detectmanytomany:"true" ) 

            hbm2java(ejb3:true)
        } 
    }
  }
}

revengfile 属性 覆盖 propertyfile。我检查了 documentation,有一个 hibernate.reveng.xml 的例子,但它实际上只适用于 hibernate.cfg.xml 文件。

这是我的 hibernate.cfg.xml 文件:

<?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://0.0.0.0:3308/db</property>
        <property name="hibernate.connection.username">user</property>
        <property name="hibernate.connection.password">pw</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.connection.zeroDateTimeBehavior">convertToNull</property>
    </session-factory>

  </hibernate-configuration>

我的 hibernate.reveng.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering
    SYSTEM "http://hibernate.org/dtd/hibernate-reverse-engineering-3.0.dtd">

<hibernate-reverse-engineering>

   <schema-selection match-catalog=".*" match-schema=".*" match-table=".*" />

   <table schema="db" name="string" class="StringEntity"></table> 

</hibernate-reverse-engineering>

对我来说这似乎是一个错误,因为 revengfile 应该期望 hibernate.reveng.xml 而不是 hibernate.cfg.xml 文件。对于遇到类似问题的任何人,我找到了一个替代解决方案来自定义逆向工程的配置。 jdbcconfiguration 中的 reversestrategy 属性 期望扩展 org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy.

的 class

这是我的 Java 逆向工程实现:

public class HibernateReverseEngineeringStrategy extends DelegatingReverseEngineeringStrategy {

public HibernateReverseEngineeringStrategy(ReverseEngineeringStrategy delegate) {
    super(delegate);
}

@Override
public String tableToClassName(TableIdentifier tableIdentifier) {
    return super.tableToClassName(tableIdentifier) + "Entity";
}

}

这是我的 gradle 蚂蚁任务:

task hibernate{
description "Generates Java Classes from the Database Schema."
group "Hibernate"

doLast{
    ant {
        def base = "$projectDir/src/main/java"

        taskdef(name: 'hibernatetool', 
            classname: 'org.hibernate.tool.ant.HibernateToolTask',
            classpath: configurations.compile.asPath )

        hibernatetool( destdir : "$base") { 

            // needs to be specified in order to find the java class
            classpath{
                path(location:"${buildDir}/classes/java/main/")
            }

            jdbcconfiguration(
                propertyfile:"src/main/resources/hibernate.properties", 
                reversestrategy:"mypackage.HibernateReverseEngineeringStrategy",
                packagename: springProperties["hibernate.generate.package"]
            ) 

            hbm2java(ejb3:true)
        } 
    }
}

}