gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 01:53:13 27 4
gpt4 key购买 nike

如何从带有字段级别注释的表生成休眠域类?我使用了 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/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com