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

发布于 2024-08-27 08:15:16 字数 1381 浏览 5 评论 0原文

我当前使用 hibernate 的设置使用 hibernate.reveng.xml 文件来生成各种 hbm.xml 文件。然后使用 hbm2java 将其转换为 POJO。我们在设计模式时花了一些时间,在表和列上放置一些相当不错的描述。使用 hbm2jhbmxml 生成这些描述时,我可以将这些描述提取到 hbm.xml 文件中。

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

<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>

So how do I talk hbm2java to pull and place those comments in thecreated Java files?

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

My current setup using hibernate uses the hibernate.reveng.xml file to generate the various hbm.xml files. Which are then turned into POJOs using hbm2java. We spent some time while designing our schema, to place some pretty decent descriptions on the Tables and there columns. I am able to pull these descriptions into the hbm.xml files when generating them using hbm2jhbmxml.

So I get something similar to this:

<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>

So how do I tell hbm2java to pull and place these comments in the created Java files?

I have read over this about editing the freemarker templates to change the way code is generated. I under stand the concept, but it was not to detailed about what else you could do with it beyond there example of pre and post conditions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲凉≈ 2024-09-03 08:15:16

在生成的 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> 

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

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

例如,在 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>

等等。

The usual way to add javadoc in generated POJOs is to use meta tags, like in this sample:

<!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> 

So, to get something similar but including the comments of your tables and columns, my understanding of the Javadoc Comments in POJOs thread is that you'd have to modify the templates used to generate hbm files.

To do so, look at the freemarker templates of hibernate-tools.jar, hbm/persistentclass.hbm.ftl, hbm/property.hbm.ftl, etc (this is not an exhaustive list) and modify them.

For example, in hbm/persistentclass.hbm.ftl, instead of:

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

I guess that you could do:

<#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>

And so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文