【问题标题】:Add properties to getter-annotations in Freemarker reverse engineering templates?在 Freemarker 逆向工程模板中向 getter-annotations 添加属性?
【发布时间】:2012-08-28 03:38:07
【问题描述】:


我正在尝试实现一个 FreeMarker 自定义逆向工程模板,该模板会自动创建我的 Hibernate 类。
在构建过程中,模板被 hibernate-tools 用来生成休眠类。
到目前为止,我为此目的使用了默认的 freemarker 模板,并且效果很好。

但现在我面临以下问题:
如何向默认的 getter-annotations 添加其他属性

One-to-may 关联的默认 freemarker 方法是(在 Ejb3PropertyGetAnnotation.ftl 中实现):

...
<#elseif c2h.isCollection(property)>
    ${pojo.generateCollectionAnnotation(property, cfg)}
...

生成的java代码例如:

@OneToMany(fetch=FetchType.LAZY, mappedBy="person")      
public Set<ContactInformation> getContactInformations() {
    return this.contactInformations;
}

但我想像这样将 cascade = CascadeType.ALL 添加到每个一对多的 g​​etter 注释中:

@OneToMany(cascade = CascadeType.ALL
           fetch=FetchType.LAZY, mappedBy="person")

我是 freemarker 和 hibernate 的新手,不知道如何存档。

非常感谢您的帮助!

【问题讨论】:

    标签: hibernate reverse-engineering freemarker hibernate-tools


    【解决方案1】:

    我发现,注释

    cascade = CascadeType.All  
    

    不一定必须在@OneToMany 方法的签名中。

    解决方法是在Freemarker模板文件Ejb3PropertyGetAnnotation.ftl中加入下面一行:

       @${pojo.importType("org.hibernate.annotations.Cascade")}(value=${pojo.importType("org.hibernate.annotations.CascadeType")}.ALL) 
    

    @OneToMany 的所有方法模板看起来像这样

    <#elseif c2h.isCollection(property)>
       ${pojo.generateCollectionAnnotation(property, cfg)}
       @${pojo.importType("org.hibernate.annotations.Cascade")}(value=${pojo.importType("org.hibernate.annotations.CascadeType")}.ALL)                    
    <#else> 
    

    结果是 f。一:

      @OneToMany(fetch=FetchType.LAZY, mappedBy="person")
      @Cascade(value=CascadeType.ALL)                    
      public Set<ContactInformation> getContactInformations() {
           return this.contactInformations;
      }             
    

    【讨论】:

      【解决方案2】:

      答案更简单:只需将 ${property.setCascade("ALL")} 放在 generateCollectionAnnotation 方法调用之前。

      <#elseif c2h.isCollection(property)>
      ${property.setCascade("ALL")}
      ${pojo.generateCollectionAnnotation(property, cfg)}
      

      而且结果要好得多,因为它使用了 javax.persistence.CascadeType 枚举。

      @OneToMany(cascade = CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="person")
      public Set<ContactInformation> getContactInformations() {
      

      可以使用的级联类型列表:

      ${property.setCascade("persist, merge, delete, refresh")}
      

      结果:

      @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH }, fetch=FetchType.LAZY, mappedBy="person") 
      public Set<ContactInformation> getContactInformations() {
      

      此解决方案还为多对多关系生成级联类型。如果你不喜欢这种行为,你需要控制它。可以生成 @OneToMany orphanRemoval 属性,但不在问题范围内。

      亲切的问候。

      【讨论】:

      • 我无法测试这个,因为我不再从事这个项目了。但是您的答案看起来更合适!谢谢。
      • 不客气@Pascal。效果很好,我正在一个项目中使用它。
      猜你喜欢
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 2014-06-11
      • 2011-08-08
      • 2012-08-26
      • 2018-07-30
      • 2013-08-12
      相关资源
      最近更新 更多