Well, it's a work in progress, but here are some examples:
Strategy file (snippets):
Code:
public class Strategy extends DelegatingReverseEngineeringStrategy {
// Exclude these columns from being mapped in the pojos
private static String[] excludePojoColumnNames = {
"CREATED_BY", // superclassed auditing information
"CREATED_DATE", // superclassed auditing information
"MODIFIED_BY", // superclassed auditing information
"MODIFIED_DATE" // superclassed auditing information
};
static {
Arrays.sort(excludePojoColumnNames);
}
@Override
public Map tableToMetaAttributes(TableIdentifier tableIdentifier) {
Map<String, MetaAttribute> metaAttributes = new HashMap<String, MetaAttribute>();
// Generate the class-code meta attribute to add a default
// serialVersionUID
MetaAttribute classCode = new MetaAttribute("class-code");
classCode.addValue("private static final long serialVersionUID = 1L;");
metaAttributes.put("class-code", classCode);
// Generate the extends meta attribute to extend all domain classes from
// BaseModel
MetaAttribute extendsCode = new MetaAttribute("extends");
extendsCode.addValue("BaseModel");
metaAttributes.put("extends", extendsCode);
return metaAttributes;
}
@Override
public Map columnToMetaAttributes(TableIdentifier identifier, String column) {
Map<String, MetaAttribute> metaAttributes = new HashMap<String, MetaAttribute>();
// Suppress pojo mapping of specified column names
if (Arrays.binarySearch(excludePojoColumnNames, column) >= 0) {
MetaAttribute genProperty = new MetaAttribute("gen-property");
genProperty.addValue("false");
metaAttributes.put("gen-property", genProperty);
}
return metaAttributes;
}
}
As you can see, I'm generating meta attributes to extend the classes from BaseModel, generate class code for the serialVersionUID and supressing the mappings for column names designated in the excludePojoColumnNames array.
dao_intf.ftl:
Code:
package org.package.${project_name}.framework.dao;
// Generated ${date}
<#assign modelName = pojo.importType(pojo.getQualifiedDeclarationName())>
<#assign thisClassName = "${modelName}Dao">
<#assign classbody>import org.package.${project_name}.framework.model.*;
/**
* Dao interface for domain model class ${modelName}
*/
public interface ${thisClassName} extends BaseDao<${modelName},${pojo.importType(c2j.getJavaTypeName(clazz.identifierProperty, false))}> {
}
</#assign>
${pojo.generateImports()}
${classbody}
This template could be used to generate the dao interfaces.
dao_hibernate.ftl:
Code:
package org.package.${project_name}.framework.dao.hibernate;
// Generated ${date}
<#assign modelName = pojo.importType(pojo.getQualifiedDeclarationName())>
<#assign thisClassName = "${modelName}DaoHibernate">
<#assign pojoInstanceName = "${modelName?uncap_first}">
<#assign daoInterfaceName = pojo.importType("org.package.${project_name}.framework.dao.${modelName}Dao")>
<#assign criterionClass = pojo.importType("org.hibernate.criterion.Criterion")>
<#assign classbody>import org.package.${project_name}.framework.model.*;
/**
* Dao implementation for domain model class ${modelName}.
*/
public class ${thisClassName} extends ${pojo.importType("org.package.${project_name}.framework.dao.hibernate.BaseHibernateDao")}<${modelName},${pojo.importType(c2j.getJavaTypeName(clazz.identifierProperty, false))}> implements ${daoInterfaceName} {
public static final long serialVersionUID = 1L;
/* ************************************** */
/* Criterion building methods */
/* ************************************** */
<#foreach property in pojo.getAllPropertiesIterator()>
<#if (!c2h.isCollection(property)) && (property.getName() != "id")><#assign propertyType = pojo.importType(c2j.getJavaTypeName(property, true))>
protected final ${criterionClass} build${pojo.getPropertyName(property)}ValueRestriction(${propertyType} ${property.getName()}) {
return buildPropertyValueRestriction("${property.getName()}", ${property.getName()});
}
protected final ${criterionClass} build${pojo.getPropertyName(property)}RangeRestriction(${propertyType} startValue, ${propertyType} endValue) {
return buildPropertyRangeRestriction("${property.getName()}", startValue, endValue);
}
</#if>
</#foreach>
}
</#assign>
${pojo.generateImports()}
${classbody}
This template could be used to generate the Hibernate specific DAO implementations. I no longer use this, but when I did, it generated Criterion builder methods.
The Spring configuration templates are way too large to post here.
I use the Ant tasks to build the project, but the build.xml and buld.properties are very large. Here are some snippets from the build.xml (note: most variables are set in the build.properties file):
Code:
<!-- ================================================================
target: mapping
The target to generate mapping (hbm.xml) files and the
Hibernate configuration file (used for configuration of
other hbm2x tasks)
================================================================ -->
<target name="mapping" depends="init" description="Generate Hibernate mapping files, Hibernate configuration file and the Spring configuration file">
<echo message="***** Building the mapping (*.hbm.xml) and configuration (hibernate.cfg.xml, applicationContext.xml) files... *****"/>
<hibernatetool destdir="${dir.javasource}">
<jdbcconfiguration packagename="org.package.${project.name}.framework.model" propertyfile="build.properties" reversestrategy="org.package.${project.name}.reveng.Strategy" revengfile="hibernate.reveng.xml"/>
<hbm2hbmxml templatepath="${dir.template}"/>
<hbm2cfgxml destdir="${dir.dest.cfgxml}"/>
</hibernatetool>
<sequential>
<echo message="***** ...mapping and configuration files sucessfully generated, refreshing... *****"/>
<antcall target="refresh"/>
<echo message="***** ...complete! *****"/>
</sequential>
</target>
<!-- ================================================================
target: pojo
The target to generate the POJOs for the Hibernate mapping
files. Note that the Hibernate configuration file must have
already been generated/supplied
================================================================ -->
<target name="pojo" depends="init" description="Generate the Hibernate domain classes (POJO's)">
<echo message="***** Building the POJO files... *****"/>
<hibernatetool destdir="${dir.javasource}">
<configuration configurationfile="${dir.dest.cfgxml}/hibernate.cfg.xml"/>
<hbm2java templatepath="${dir.template}" jdk5="true"/>
</hibernatetool>
<sequential>
<echo message="***** ...POJO files sucessfully generated, refreshing... *****"/>
<antcall target="refresh"/>
<echo message="***** ...POJO file generation complete! *****"/>
</sequential>
</target>
<!-- ================================================================
target: dao
The target to generate the DAO interface and implementation
classes. note that the hibernate configuration gfile must
have already been generated/supplied
================================================================ -->
<target name="dao" depends="init" description="Generate the DAO interface and implementation classes">
<echo message="***** Building the DAO interface and implementation files... *****"/>
<hibernatetool>
<configuration configurationfile="${dir.javasource}/hibernate.cfg.xml"/>
<hbmtemplate
templatepath="${dir.template.dao}"
template="${dao.intf.templatename}"
destdir="${dir.dest.dao.interface}"
filepattern="${dao.intf.filepattern}">
<propertyset refid="dao.properties"/>
</hbmtemplate>
<hbmtemplate
templatepath="${dir.template.dao}"
template="${dao.hibernate.templatename}"
destdir="${dir.dest.dao.hibernate}"
filepattern="${dao.hibernate.filepattern}">
<propertyset refid="dao.properties"/>
</hbmtemplate>
</hibernatetool>
<sequential>
<echo message="***** ...DAO interface and implementation files sucessfully generated, refreshing... *****"/>
<antcall target="refresh"/>
<echo message="***** ...DAO interface and implementation file generation complete! *****"/>
</sequential>
</target>
<!-- ================================================================
target: spring
The target to generate the Spring configuration file. Note
that the Hibernate configuration file must have already been
generated/supplied
================================================================ -->
<target name="spring" depends="init" description="Generate the Spring configuration file">
<echo message="***** Building the Spring configuration file ... *****"/>
<echo message="Building templates for the following: ${spring.templates}"/>
<for list="${spring.templates}" param="templatename">
<sequential>
<hibernatetool>
<configuration configurationfile="${dir.javasource}/hibernate.cfg.xml"/>
<hbmtemplate
templatepath="${dir.template.spring}"
template="spring-@{templatename}.ftl"
destdir="${dir.dest.spring}"
filepattern="applicationContext-@{templatename}.xml">
<propertyset refid="spring.properties"/>
</hbmtemplate>
</hibernatetool>
</sequential>
</for>
<sequential>
<echo message="***** ...Spring configuration file sucessfully generated, refreshing *****"/>
<antcall target="refresh"/>
<echo message="***** ...Spring configuration file generation complete! *****"/>
</sequential>
</target>
This is just a taste of my reverse engineering configurations, let me know how much more information you need.