Introduction

This page resumes the configuration possibility to customize your code.
The reader will learn how to apply custom naming convention for package, entity, field.

Of course, conventions are not limited. I propose a number of 'standard' one that I find useful. But other can be added on the future.


Naming convention

Package naming convention

Instead of generating in a flat directory structure, entities (tables/views) can be grouped in specific package.
It gives cleaner code when classifing entities by business interest.
Let say that each entity:
  • starting with ADMIN goes in 'administration' package;
  • starting with FIN_ goes in 'finance' package;
  • etc...
  • and by default goes to a package 'other'.
Adding the following snippet to your configuration will help achieving it
<business-model>
 <business-package default="other">
  <condition type="package" startsWith="ADMIN" result="administration"></condition>
  <condition type="package" startsWith="FIN_" result="finance"></condition>
 </business-package>
...
</business-model>

Entity naming

There are 2 ways to change the name of a entity:
  • by affecting the entity (table/view) specific name with an alias (the format of the alias is uppercase, underscore)
  • by providing a global convention on entities, currently there is one:
    • Apply-strip-entity-naming-convention that:
      • remove the prefix or suffix of a table if it match a certain pattern.
The name of the JPA class will be change accordingly by the mapping remains with the entity name of the database.

Specific entity naming

The following snippet
<enrichment>
...
<entity name="ITEM" alias="MY_GOOD_ITEM">
...
</enrichment>
will create a class MyGoodItem like
@Entity (name="MyGoodItem")
@Table (name="ITEM")
public class MyGoodItem {
 
    @Id @Column(name="ITEMID" )
    private Integer itemid;
And all the reference to this class will be adapted accordingly.

Entity naming convention

Apply-strip-entity-naming-convention convention will remove entity whose name start or end with a certain pattern.
In the following snippet
<enrichment>
...
 <conventions>
  <entity-naming-convention type="apply-strip-table-name-prefix" pattern-to-strip="SYS,FIN"/>
  <entity-naming-convention type="apply-strip-table-name-suffix" pattern-to-strip="_TABLE,_VIEW"/>
 </conventions>
...
</enrichment>
will cause:
  • entity named SYS_ADMIN_LOG to be converted to AdminLog class
  • entity named AIRPORT_TABLE to be converted to Airport class.

Field naming

There are 2 ways to change the name of a field:
  • by affecting the field specific name with an alias (the format of the alias is uppercase, underscore)
  • by providing a global convention on entities, currently there are two:
    • Apply-strip-column-naming-convention that:
      • remove the prefix or suffix of a column if it match a certain pattern.
    • Apply-referenced-alias-when-no-ambiguity
      • when there is no potential ambiguity (that would lead to duplicate variable and thus compilation error), it propose a simplification of the variable name.
      • The ambiguity comes if one table A has more than one FK referencing B then the variable in class B corresponding to the collection of Class A cannot be named 'as' (s for plural).
      • But when there is no ambiguity the B can have a variable 'as' corresponding to a collection of class A element.

Specific entity naming

The following snippet
<enrichment>
...
 <entity name="ITEM" alias="MY_GOOD_ITEM">
  <field name="PRODUCTID" alias="THIS_IS_MY_PRODUCT" ></field>
 </entity>
...
</enrichment>
will create a the variable thisIsMyProduct in class MyGoodItem like
    @ManyToOne (fetch=FetchType.LAZY , optional=false)
    @JoinColumn(name="PRODUCTID", nullable=false,  unique=false )
    private Product thisIsMyProduct;
 
The getter/setter are adapted accordingly

Field naming convention

apply-strip-column-naming-convention convention will remove entity whose name start or end with a certain pattern.
In the following snippet
<enrichment>
...
 <conventions>
  <column-naming-convention type="apply-strip-column-name-suffix" pattern-to-strip="ID,PK,FK">
 </conventions>
...
</enrichment>
Will create variable stripped form there ending if it match ID or PK or FK.
So if you have convention that match ID, PK, FK for describing primary key and foreign key column and that you do not want to have them in the generated code this convention will do it.
    @Id @Column(name="ITEMID" )
    private Integer item;
But the mapping remains with the correct field name.

apply-referenced-alias-when-no-ambiguity convention will create 'natural' name (i.e. coming from the entity) when there is no ambiguity.
In the following snippet
<enrichment>
...
 <conventions>
  <reference-naming-convention type="apply-referenced-alias-when-no-ambiguity" is-to-plurialize="true"></reference-naming-convention>
...
</enrichment
Will create
    private Set <Products> products = new HashSet<MyGoodItem>()

apply-fix-primary-key-column-name-when-no-ambiguity convention will create a primary key variable based on the default value if it is not already present
<conventions>
...
<column-naming-convention type="apply-fix-primary-key-column-name-when-no-ambiguity" default-value="ID"/>
...
</conventions>
Will create
    @Id @Column(name="ITEMID" )
    private Integer id;

Summary

Sample configuration for the petshop project that resume the configuration feature mention above.

<!DOCTYPE root>
<generator-config>
    <configuration>
        <model name="petshop" version="1.0" package-root="net.sf.mp.demo.jpa2">
            <data-model>
                <fileSource name="petshop" dir="c:/MinuteProject/data">
                </fileSource>
                <dataSource>
                    <driverClassName>org.hsqldb.jdbcDriver</driverClassName>
                    <url>jdbc:hsqldb:hsql://127.0.0.1:9001/petshop</url>
                    <username>sa</username>
                    <password></password>
                </dataSource>
                <!-- for Oracle and DB2 please set the schema
                <schema> </schema>
                 -->
                <primaryKeyPolicy oneGlobal="true" oneForEachTable="false">
                    <primaryKeyPolicyPattern prefix="" suffix="" name="sequencePattern" sequenceName="hibernate_sequence"></primaryKeyPolicyPattern>
                </primaryKeyPolicy>
            </data-model>
            <business-model>
                <generation-condition>
                    <condition type="exclude" startsWith="DUAL"></condition>
                    <condition type="exclude" startsWith="ID_GEN"></condition>
                </generation-condition>
                <business-package default="pet">
                    <condition type="package" startsWith="PRODUCT" result="product"></condition>
                    <condition type="package" startsWith="ITEM" result="product"></condition>
                </business-package>
                <enrichment>
                   <conventions>
                      <column-naming-convention type="apply-strip-column-name-suffix" pattern-to-strip="ID">
                      </column-naming-convention>
                      <reference-naming-convention type="apply-referenced-alias-when-no-ambiguity" is-to-plurialize="true"></reference-naming-convention>
                   </conventions>
                    <package name="product">
                        <entity-group entities="PRODUCT"></entity-group>
                        <entity-group entities="ITEM"></entity-group>
                    </package>
                    <entity name="PRODUCT" alias="MY_GOOD_PRODUCT">
                    </entity>
                    <entity name="ITEM" alias="MY_GOOD_ITEM">
                        <field name="PRODUCTID" alias="THIS_IS_MY_PRODUCT" ></field>
                    </entity>
                    <entity name="CATEGORY" content-type="reference-data" >
                       <semantic-reference>
                         <sql-path path="NAME"/>
                       </semantic-reference>
                    </entity>
                </enrichment>
            </business-model>
        </model>
        <targets catalog-entry="JPA2"/>
    </configuration>
</generator-config>