2010-03-29 40 views
4

我當前使用休眠的設置使用hibernate.reveng.xml文件來生成各種hbm.xml文件。然後使用hbm2java變成POJO。我們花了一些時間設計我們的模式,在表格和列上放置一些相當不錯的描述。當使用hbm2jhbmxml生成文件時,我可以將這些描述放入hbm.xml文件中。如何在hbm2java中創建註釋創建POJO?

所以我得到類似這樣:

<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有關編輯freemarker模板以更改生成代碼的方式。我承認這個概念,但並不是要詳細說明除了事前和事後條件的例子之外,你還可以用它做什麼。

回答

3

通常的方法所產生的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文件的模板。

要做到這一點,看看休眠-的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> 

我想,你可以這樣做:

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

等。

+0

@Pascal看起來像我在找什麼。一旦我能夠真正嘗試它,我會回來接受它。謝謝您的幫助。 – 2010-04-01 17:54:51