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

Jac*_*oen 5 java orm hibernate hbm2java

我当前使用 hibernate 的设置使用该hibernate.reveng.xml文件生成各种hbm.xml文件。然后使用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>
Run Code Online (Sandbox Code Playgroud)

那么我如何告诉hbm2java在创建的 Java 文件中提取和放置这些注释呢?

我已阅读过有关编辑freemarker的模板来更改生成代码的方式。我理解这个概念,但没有详细说明除了前置条件和后置条件示例之外你还能用它做什么。

Pas*_*ent 4

在生成的 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> 
Run Code Online (Sandbox Code Playgroud)

因此,要获得类似的内容,但包括表和列的注释,我对POJOs线程中的 Javadoc 注释的理解是,您必须修改用于生成 hbm 文件的模板。

为此,请查看hibernate-tools.jarhbm/persistentclass.hbm.ftlhbm/property.hbm.ftl等的 freemarker 模板(这不是详尽的列表)并修改它们。

例如,在 中hbm/persistentclass.hbm.ftl,而不是:

<#if clazz.table.comment?exists  && clazz.table.comment?trim?length!=0>
 <comment>${clazz.table.comment}</comment>
</#if>
Run Code Online (Sandbox Code Playgroud)

我想你可以这样做:

<#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>
Run Code Online (Sandbox Code Playgroud)

等等。