Controlling which table and column names gets used when generating JPA code

Controlling which table and column names gets used when generating JPA code

TRIMM JPA will leave table and column names up to the default JPA rules, unless you instruct it to specify @Table, @JoinTable, @Column, @JoinColumn, et, e.g. using the addTableAndColumnNames parameter in the JPA YAML configuration.

It can sometimes be useful to control the table and column names if the default names (which follows the JPA default naming standard) collide with database reserved words or if you use a “modern” database like Oracle which still limits names to 30 characters 😉

If you set the addTableAndColumnNames to true then TRIMM JPA YAML based code generator (dk.tigerteam.trimm.mdsd.jpa.configuration.JpaConfigurationBasedCodeGenerator) configures the dk.tigerteam.trimm.mdsd.jpa.JpaJavaGenerator to use the dk.tigerteam.trimm.mdsd.jpa.JpaNamedTablesAndColumnsListener.
If you specify nothing further TRIMM JPA will (through the JpaNamedTablesAndColumnsListener) will just insert @javax.persistence.Column, @javax.persistence.JoinColumn, @javax.persistence.JoinTable, @javax.persistence.Table, etc. with the default names it calculates.

Typically you only need to override a few names. In that case you can specify it directly in the YAML configuration using the resolveTableNames, resolveColumnNames, resolveAttributeOverrideColumnNames:

 addTableAndColumnNames: true
  - defaultTableName: User
    resolvedTableName: User_
  - defaultTableName: By
    resolvedTableName: By_
  - defaultColumnName: value
    resolvedColumnName: value_
  - defaultColumnName: type
    resolvedColumnName: type_
  - defaultColumnName: value_type_somethingelse
    resolvedColumnName: v_t_s

In case you have a pattern for e.g. abbreviating names or if the resolveTableNames, resolveColumnNames, resolveAttributeOverrideColumnNames section(s) simply gets too verbose then you can also use the jpaNamedTablesAndColumnsNameResolver parameter to specify either a Fully Qualified Class Name (FQCN for a class on the generator classpath that implements the dk.tigerteam.trimm.mdsd.jpa.JpaNamedTablesAndColumnsNameResolver interface.

 addTableAndColumnNames: true
 jpaNamedTablesAndColumnsNameResolver: dk.tigerteam.trimm.myownextensions.MyTablesAndColumnsNameResolver

You can also specify a project relative path for a .groovy script which contains a class which implements the dk.tigerteam.trimm.mdsd.jpa.JpaNamedTablesAndColumnsNameResolver interface:

 addTableAndColumnNames: true
 jpaNamedTablesAndColumnsNameResolver: model/DatabaseTableNameResolver.groovy

Example of a custom :

import dk.tigerteam.trimm.mdsd.jpa.JpaNamedTablesAndColumnsNameResolver

 * Example JpaNamedTablesAndColumnsNameResolver script which makes all generated names
 * compatible with Oracle (e.g. no names over 30 characters) and some protected keywords renamed
public class MyTablesAndColumnsNameResolver implements JpaNamedTablesAndColumnsNameResolver {
	public String resolveJoinColumnName(PropertyEvent event, String defaultJoinColumnName) {
		return defaultJoinColumnName.substring(0, defaultJoinColumnName.length() > 30 ? 30 : defaultJoinColumnName.length());
	public String resolveColumnName(PropertyEvent event, String defaultColumnName) {
		if (defaultColumnName == "by") return "by_";
		if (defaultColumnName == "key") return "key_";
		if (defaultColumnName == "value") return "value_";
		if (defaultColumnName == "type") return "type_";
		return defaultColumnName.substring(0, defaultColumnName.length() > 30 ? 30 : defaultColumnName.length());
	public String resolveTableName(ClazzEvent event, String defaultTableName) {
		return defaultTableName.substring(0, defaultTableName.length() > 30 ? 30 : defaultTableName.length());
	public String resolveJoinTableName(PropertyEvent event, String defaultJoinTableName) {
		return defaultJoinTableName.substring(0, defaultJoinTableName.length() > 30 ? 30 : defaultJoinTableName.length());
	public String resolveInverseJoinColumnName(AssociationEvent event, String defaultInverseJoinColumnName) {
		return defaultInverseJoinColumnName.substring(0, defaultInverseJoinColumnName.length() > 30 ? 30 : defaultInverseJoinColumnName.length());
	public String resolveAttributeOverrideColumnName(PropertyEvent event, Property overrideAttributeProperty,
	String overrideAttributeName, String defaultAttributeOverrideColumnName) {
		return defaultAttributeOverrideColumnName.substring(0, defaultAttributeOverrideColumnName.length() > 30 ? 30 : defaultAttributeOverrideColumnName.length());
	public String resolveIndexColumnName(PropertyEvent event, String defaultIndexColumnName) {
		return defaultIndexColumnName.substring(0, defaultIndexColumnName.length() > 30 ? 30 : defaultIndexColumnName.length());


No comments yet.