【问题标题】:Using custom templates with Hibernate tools reverse engineering from Gradle将自定义模板与来自 Gradle 的 Hibernate 工具进行逆向工程
【发布时间】:2013-12-11 19:08:19
【问题描述】:

使用 Gradle 中的 Hibernate ant 任务,我可以使用 http://docs.jboss.org/tools/latest/en/hibernatetools/html_single/index.html#d0e5102 的文档从数据库中生成实体类

当我更改template 和templateprefix 时,Hibernate 找不到我的自定义模板。可以肯定的是,我直接从 hibernate jar 中复制了模板,并在 Pojo.ftl 中添加了一条注释。

这是我使用 Hibernate 模板的工作 Gradle 构建文件...在工作构建文件下方是失败的构建文件,除了 template 和 templateprefix 之外,它是相同的。

apply plugin: 'java'

repositories {
    mavenCentral()
}

configurations {
    reverseMap
}

dependencies {
    reverseMap 'org.hibernate:hibernate-tools:4.0.0-CR1'
    reverseMap 'org.slf4j:slf4j-simple:1.7.5'
    reverseMap files('/Users/jzwolak/local/lib/ojdbc6-11.2.0.2.0.jar')
    compile 'javax:javaee-api:7.0'
}

project.ext {
    hibernateRevEngXml = "$projectDir/config/hibernate.reveng.xml"
    hibernateDestDir = file("$buildDir/generated")
}

task reverseMap {
    inputs.files hibernateRevEngXml
    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/config/hibernate.cfg.xml",
                        revengfile:hibernateRevEngXml,
                        packagename:
                                "com.mybiz"
                        //reversestrategy="ReverseEngineeringStrategy classname"
                        //detectmanytomany="true|false"
                        //detectoptmisticlock="true|false"
                )
                hbmtemplate(
                    templateprefix:"pojo/" ,
                    template:"pojo/Pojo.ftl", 
                    filepattern:"{package-name}/{class-name}.java"
                ) {
                    property(key:"jdk5",value:"true")
                    property(key:"ejb3",value:"true")
                }
                /*
                hbm2java(
                        jdk5: true,
                        ejb3: true
                )
                */
                // Adds the config directory to the path so that log4j can pick up
                // its properties file.
                classpath {
                    pathelement( path: "config" )
                }
            }
        }
    }
}

compileJava.source reverseMap.outputs.files, sourceSets.main.java

这是失败的构建文件。

apply plugin: 'java'

repositories {
    mavenCentral()
}

configurations {
    reverseMap
}

dependencies {
    reverseMap 'org.hibernate:hibernate-tools:4.0.0-CR1'
    reverseMap 'org.slf4j:slf4j-simple:1.7.5'
    reverseMap files('/Users/jzwolak/local/lib/ojdbc6-11.2.0.2.0.jar')
    compile 'javax:javaee-api:7.0'
}

project.ext {
    hibernateRevEngXml = "$projectDir/config/hibernate.reveng.xml"
    hibernateDestDir = file("$buildDir/generated")
}

task reverseMap {
    inputs.files hibernateRevEngXml
    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/config/hibernate.cfg.xml",
                        revengfile:hibernateRevEngXml,
                        packagename:
                                "com.mybiz"
                        //reversestrategy="ReverseEngineeringStrategy classname"
                        //detectmanytomany="true|false"
                        //detectoptmisticlock="true|false"
                )
                hbmtemplate(
                    templateprefix:"templates/custom_pojo/" ,
                    template:"templates/custom_pojo/Pojo.ftl", 
                    filepattern:"{package-name}/{class-name}.java"
                ) {
                    property(key:"jdk5",value:"true")
                    property(key:"ejb3",value:"true")
                }
                /*
                hbm2java(
                        jdk5: true,
                        ejb3: true
                )
                */
                // Adds the config directory to the path so that log4j can pick up
                // its properties file.
                classpath {
                    pathelement( path: "config" )
                }
            }
        }
    }
}

compileJava.source reverseMap.outputs.files, sourceSets.main.java

这是目录树

build.gradle
config
|    hibernate.cfg.xml
|    hibernate.reveng.xml
|    log4j.properties
templates
|    custom_pojo
|    |    Ejb3PropertyGetAnnotation.ftl
|    |    Ejb3TypeDeclaration.ftl
|    |    GetPropertyAnnotation.ftl
|    |    Pojo.ftl
|    |    PojoConstructors.ftl
|    |    PojoEqualsHashcode.ftl
|    |    PojoExtraClassCode.ftl
|    |    PojoFields.ftl
|    |    PojoInterfacePropertyAccessors.ftl
|    |    PojoPropertyAccessors.ftl
|    |    PojoToString.ftl
|    |    PojoTypeDeclaration.ftl
|    |    test.ftl

这是错误

[ant:hibernatetool] org.hibernate.tool.hbm2x.ExporterException: Error while processing Entity: com.mybiz.TsModelRealizationView with template templates/custom_pojo/Pojo.ftl
[ant:hibernatetool] java.io.FileNotFoundException: Template templates/custom_pojo/Pojo.ftl not found.
:reverseMap FAILED

我尝试将. 和templates 与config 一起添加到类路径中,它们已经在类路径中并且可以正常工作。

我已经为template 和templateprefix 尝试了多种组合,但唯一有效的是上面第一个 build.gradle 文件中的组合。

更新

我尝试使用 templateprefix 和 template 的绝对路径,并得到相同的文件未找到错误。

【问题讨论】:

    标签: hibernate templates


    【解决方案1】:

    添加

    reverseMap files('.')
    

    到您的依赖项。

    【讨论】:

    • 为什么?这是什么 ?从 hbm.xml 文件生成带注释的代码时,我遇到了同样的错误,而且我没有任何 build.xml...
    【解决方案2】:

    有点晚了,但如果仍然有帮助,这是指定模板基本路径的另一种方法:

    改变这个:

    hibernatetool( destdir : hibernateDestDir )
    

    到:

    hibernatetool( destdir : hibernateDestDir, templatepath : 'templates' )
    

    模板部分应该是:

    templateprefix:"custom_pojo/" ,
    template:"custom_pojo/Pojo.ftl", 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-14
      • 2012-03-15
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-07
      • 1970-01-01
      相关资源
      最近更新 更多