AlgoTraderAlgoTrader Documentation

Chapter 10. Code Generation

Java Entities, Entity Interfaces and Value Objects are created by the means of the Hibernate Tools project using the hbm2java code exporter.

The Hibernate Tools project provides the code generator as an Eclipse plugin as well as a set of Ant tasks. AlgoTrader provides a custom maven plugin called maven-codegen-plugin which wraps the code generator.

<groupId>algotrader</groupId>
<artifactId>model-codegen-plugin</artifactId>
<name>Model code generator plugin</name>
<version>0.1.5</version>

The maven-codegen-plugin has been added to the file /algotrader/common/pom.xml

<plugin>
    <groupId>algotrader</groupId>
    <artifactId>model-codegen-plugin</artifactId>
    <version>0.1.5</version>
    <executions>
        <execution>
            <id>generate-model</id>
            <goals>
                <goal>codegen</goal>
            </goals>
            <configuration>
                <templates>
                    <template>
                        <file>pojo/Pojo.ftl</file>
                        <pattern>{package-name}/{class-name}.java</pattern>
                    </template>
                    <template>
                        <file>pojo/Interface.ftl</file>
                        <pattern>{package-name}/{class-name}I.java</pattern>
                    </template>
                    <template>
                        <file>pojo/ValueObject.ftl</file>
                        <pattern>{package-name}/{class-name}VO.java</pattern>
                    </template>
                    <template>
                        <file>pojo/ValueObjectBuilder.ftl</file>
                        <pattern>{package-name}/{class-name}VOBuilder.java</pattern>
                    </template>
                </templates>
            </configuration>
        </execution>
    </executions>
</plugin>

The above configuration generates the following artifacts for each Java Entity defined in the Hibernate mapping files:

The code generator uses Hibernate mapping files which are located in /algotrader/common/src/main/resources in combination with Freemarker templates which are located in /algotrader/common/pojo . These templates are based on the original version supplied with the Hibernate Tools project but have been augmented to produces Java code needed by AlgoTrader. For this purpose several custom attributes have been added to Hibernate mapping files:

Table 10.1. Hibernate mapping files - custom meta attributes

TagDescription
implementsThe interface this Entity implements
generated-className of the main Entity class file
class-codeExtra class code generated into Entity classes
interface-codeExtra class code generated into Entity interfaces
vo-codeExtra class code generated into Value Objects
class-descriptionJavaDoc comment for classes
field-descriptionJavaDoc comment for fields
use-in-equalsshould this property be used in the equals method
property-typethe class to be used for this property


Generated code is placed under the directory /algotrader/common//target/generated-sources/main/java.