代码之家  ›  专栏  ›  技术社区  ›  Jacob Schoen

如何在hbm2java创建的pojo中生成注释?

  •  4
  • Jacob Schoen  · 技术社区  · 14 年前

    我当前使用Hibernate的设置使用 hibernate.reveng.xml 文件以生成 hbm.xml 文件夹。然后变成pojos使用 hbm2java . 我们花了一些时间设计我们的模式,在表和列中放置一些相当不错的描述。我可以将这些描述 HBM.XML 生成文件时使用 hbm2jhbmxml .

    所以我得到了类似的东西:

    <class name="test.Person" table="PERSONS">
      <comment>The comment about the PERSONS table.</comment>
      <property name="firstName" type="string">
          <column name="FIRST_NAME" length="100" not-null="true">
              <comment>The first name of this person.</comment>
          </column>
      </property>
      <property name="middleInitial" type="string">
          <column name="MIDDLE_INITIAL" length="1">
              <comment>The middle initial of this person.</comment>
          </column>
      </property>
      <property name="lastName" type="string">
          <column name="LAST_NAME" length="100">
              <comment>The last name of this person.</comment>
          </column>
      </property>
    </class>
    

    那我该怎么说呢 HBM2JAVA 在创建的Java文件中拖放这些注释?

    我读过了 this 关于编辑自由标记模板以更改代码生成方式。我同意这个概念,但它并没有详细说明你能用它做什么,除了前面和后面的条件。

    1 回复  |  直到 14 年前
        1
  •  3
  •   Pascal Thivent    14 年前

    在生成的POJO中添加javadoc的通常方法是使用 meta 标签,如此示例中所示:

    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
    
    <class name="Person">
      <meta attribute="class-description">
      Javadoc for the Person class
      @author Frodo
      </meta>
    
      <id name="id" type="long">
        <meta attribute="scope-set">protected</meta>
        <generator class="increment"/>
      </id>
      <property name="name" type="string">
        <meta attribute="field-description">The name of the person</meta>
      </property>
    </class> 
    

    因此,要得到类似的结果,但包括表和列的注释,我对 Javadoc Comments in POJOs 线程是您必须修改用于生成HBM文件的模板。

    为此,请查看的FreeMarker模板 休眠工具.jar , hbm/persistentclass.hbm.ftl , hbm/property.hbm.ftl 等等(这不是一个详尽的列表)并修改它们。

    例如,在 HBM/PersistentClass.HBM.FTL公司 ,而不是:

    <#if clazz.table.comment?exists  && clazz.table.comment?trim?length!=0>
     <comment>${clazz.table.comment}</comment>
    </#if>
    

    我想你可以这样做:

    <#if clazz.table.comment?exists  && clazz.table.comment?trim?length!=0>
     <meta attribute="class-description">
      ${clazz.table.comment}
     </meta>
     <comment>${clazz.table.comment}</comment>
    </#if>
    

    等等。