How to generate comments in hbm2java created by POJO?

My current setup using hibernate uses the hibernate.reveng.xml file to generate various hbm.xml files. Then they turn into POJO using hbm2java . We spent some time developing our scheme in order to place fairly decent descriptions in the tables and the columns there. I can output these descriptions to hbm.xml files when they are created 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 pull and put these comments into the generated Java files?

I read this about changing freemarker templates to change the way code is created. I adhered to the concept, but was not detailed about what else you could do with it outside the example of conditions before and after.

+4
source share
1 answer

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

 <!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 comments on your tables and columns, my understanding of Javadoc Comments in POJOs is that you have to change the templates used to generate hbm files.

To do this, check out the freemarker templates 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:

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

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

Etc.

+3
source

Source: https://habr.com/ru/post/1305463/


All Articles