reverse-engineering - Hibernate - 将注释从属性(方法)级别移动到字段级别

标签 reverse-engineering hibernate-annotations

如何从带有字段级别注释的表生成休眠域类?我使用了 Hibernate Tools 项目并从数据库中的表生成了域类。生成的类在 getter 方法上而不是在字段级别上有注释。请建议一种生成带有注释字段的域类的方法。 eclipse/IDEA 等中是否有任何可用的重构工具来将注释从方法级别移动到字段级别?

感谢您的帮助和时间。

最佳答案

以下是步骤:

  • 通过在 eclipse 文件夹中执行搜索来分配“hibernate-tools.jar”
    例如,您会在C:\eclipse\plugins\org.hibernate.eclipse_3.4.1.xxx\lib\tools
  • 解压到临时文件夹(WinRar 可以做到这一点)
    例如解压到[Your Project]\template
  • 在 [Your Project]\template\pojo 文件夹下,创建一个名为“Ejb3FieldGetAnnotation.ftl”的文件

    该文件实际上是“Ejb3PropertyGetAnnotation.ftl”的副本,但所有单词“property”都被替换为“field”,因为该文件将被放置在遍历所有字段(而不是属性)的循环中。
    我在这篇文章中包含了文件的内容
  • 删除属性级注释:
    在文件“PojoPropertyAccessors.ftl”中,删除或注释掉
    <#include "GetPropertyAnnotation.ftl"/>
    
  • 添加字段级注释:
    在文件“PojoFields.ftl”中,添加
    <#include "Ejb3FieldGetAnnotation.ftl"/>
    ${pojo.getFieldModifiers(field)} ... 
    
  • 生成Java实体时,选择“使用自定义模板”并指定模板文件夹。在这种情况下,它将是 [Your Project]\template
    ==================================================================================
    Ejb3FieldGetAnnotation.ftl
    ==================================================================================
    
    <#if ejb3>
    <#if pojo.hasIdentifierProperty()>
    <#if field.equals(clazz.identifierProperty)>
     ${pojo.generateAnnIdGenerator()}
    <#-- if this is the id property (getter)-->
    <#-- explicitly set the column name for this property-->
    </#if>
    </#if>
    
    <#if c2h.isOneToOne(field)>
    ${pojo.generateOneToOneAnnotation(field, cfg)}
    <#elseif c2h.isManyToOne(field)>
    ${pojo.generateManyToOneAnnotation(field)}
    <#--TODO support optional and targetEntity-->    
    ${pojo.generateJoinColumnsAnnotation(field, cfg)}
    <#elseif c2h.isCollection(field)>
    ${pojo.generateCollectionAnnotation(field, cfg)}
    <#else>
    ${pojo.generateBasicAnnotation(field)}
    ${pojo.generateAnnColumnAnnotation(field)}
    </#if>
    </#if>
    

  • 希望它对你有用。

    关于reverse-engineering - Hibernate - 将注释从属性(方法)级别移动到字段级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1861817/

    相关文章:

    windows-ce - 确定 Windows CE 可执行文件使用的 16 位 CRC/校验和算法的方法?

    gcc - 反汇编简单二进制文件时这些额外的指令是什么?

    c++ - 在已编译的应用程序中更改嵌入式目标端口

    java - 如何在hibernate中仅审计父类(super class)的一部分?

    delphi - 如何反编译不是 .net 模块的 .exe 文件

    reverse-engineering - 如何识别用于开发软件的编程语言?

    Hibernate buildSessionFactory() 异常

    hibernate - 如何让 Hibernate 调用我的自定义 typedef?

    java - 如何在hibernate中通过注解引入ondelete级联

    java - hibernate继承映射single_table与 boolean 类型的属性