Transformatorhuis

January 27, 2009

Customizing hbm2java (Freemarker & Maven)

Filed under: Coding, Configuration — cyberroadie @ 4:24 pm

Copy the freemarker templates out the the hibernate-tools.jar into you maven directory tree (${basedir}).
In maven:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<componentProperties>
    ...
    <templatepath>${basedir}/templates</templatepath>
    <template>${basedir}/templates/pojo/Pojo.ftl</template>
</componentProperties>
</plugin>

The template needs to refer to the parent template, the rest is included automaticly.

Howto customize the freemarker templates

Example:
To pick up from a hibernate template (*.hbm.xml):

<property column="TestField" type="integer" name="testField">
    <meta attribute="annotation">@XmlElement</meta>
</property>

And generate:

@XmlElement
private Integer testField;

Add this in PojoFields.ftl:

<#if pojo.hasMetaAttribute(field, "annotation")>
${c2j.getMetaAsString(field, "annotation","\n")}
</#if>
${pojo.getFieldModifiers(field)} ${pojo.getJavaTypeName(field, jdk5)} ${field.name}<#if pojo.hasFieldInitializor(field, jdk5)> = ${pojo.getFieldInitialization(field, jdk5)}</#if>;
</#if>

Blog at WordPress.com.