Here is an XSLT and ant script I've wrote to generate DAOs from hibernate mapping files. I use it as a quick and dirty solution and hope soon there will
be support in hibernate tools to do same using velocity templates.
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<xsl:output method="text" />
<xsl:param name="genDAOlong">net.sf.jbprize.framework.dao.hibernate.GenericHibernateDAO</xsl:param>
<xsl:param name="genDAO">GenericHibernateDAO</xsl:param>
<xsl:template match="/hibernate-mapping">
<xsl:for-each select="class">
<xsl:variable name="class">
<xsl:for-each select="str:tokenize(@name, '.')">
<xsl:if test="position() = last()">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="idClass">
<xsl:for-each select="str:tokenize(id/@type|composite-id/@class, '.')">
<xsl:if test="position() = last()">
<xsl:choose>
<xsl:when test=". = 'long'">
<xsl:value-of select="'Long'"/>
</xsl:when>
<xsl:when test=". = 'string'">
<xsl:value-of select="'String'"/>
</xsl:when>
<xsl:when test=". = 'big_decimal'">
<xsl:value-of select="'BigDecimal'"/>
</xsl:when>
<xsl:when test=". = 'integer'">
<xsl:value-of select="'Integer'"/>
</xsl:when>
<xsl:when test=". = 'character'">
<xsl:value-of select="'Character'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="idClassFull">
<xsl:choose>
<xsl:when test="id/@type|composite-id/@class = 'long'">
<xsl:value-of select="'java.lang.Long'"/>
</xsl:when>
<xsl:when test="id/@type|composite-id/@class = 'string'">
<xsl:value-of select="'java.lang.String'"/>
</xsl:when>
<xsl:when test="id/@type|composite-id/@class = 'big_decimal'">
<xsl:value-of select="'java.math.BigDecimal'"/>
</xsl:when>
<xsl:when test="id/@type|composite-id/@class = 'integer'">
<xsl:value-of select="'java.lang.Integer'"/>
</xsl:when>
<xsl:when test="id/@type|composite-id/@class = 'character'">
<xsl:value-of select="'java.lang.Character'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="id/@type|composite-id/@class"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="package">
<xsl:for-each select="str:tokenize(@name, '.')">
<xsl:if test="position() != last()">
<xsl:value-of select="."/>
<xsl:if test="position() != (last() - 1)">
<xsl:value-of select="'.'"/>
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="concat('package ', $package, ';')"/>
<xsl:value-of select="'
'" />
<xsl:value-of select="'
'" />
<xsl:value-of select="concat('import ', $genDAOlong, ';')"/>
<xsl:value-of select="'
'" />
<xsl:value-of select="concat('import ', @name, ';')"/>
<xsl:value-of select="'
'" />
<xsl:value-of select="concat('import ', $idClassFull, ';')"/>
<xsl:value-of select="'
'" />
<xsl:value-of select="'
'" />
<xsl:value-of
select="concat('public class ', $class, 'DAO')" />
<xsl:value-of select="'
'" />
<xsl:value-of
select="concat(' extends ', $genDAO, '<', $class, ', ', $idClass, '> {')" />
<xsl:value-of select="'
'" />
<xsl:value-of select="concat(' public ', $class, 'DAO() {')" />
<xsl:value-of select="'
'" />
<xsl:value-of
select="concat(' super(', $class, '.class, ', $idClass, '.class);')" />
<xsl:value-of select="'
'" />
<xsl:value-of select="' }'" />
<xsl:value-of select="'
'" />
<xsl:value-of select="'}'" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Ant script
Code:
<?xml version="1.0" encoding="UTF-8"?>
<project name="DAO Generator" default="daogen">
<property name="my.root.dir" value="${basedir}" />
<property name="src.dir" value="${basedir}/src" />
<property name="build.common" value="${basedir}/build"/>
<property name="build.thirdparty" value="${basedir}/../ThirdParty"/>
<!-- Build classpath -->
<path id="classpath">
<pathelement location="${basedir}" />
<fileset dir="${build.thirdparty}/lib/xerces-xalan">
<include name="xalan.jar" />
<include name="xml-apis.jar" />
<include name="xercesImpl.jar" />
<include name="resolver.jar" />
</fileset>
</path>
<property name="build.classpath" refid="classpath" />
<target name="daogen" description="Generate DAO java source code from hibernate mappings">
<fileset dir="${src.dir}" id="hbm-files">
<include name="**/*.hbm.xml" />
</fileset>
<xmlcatalog id="catalog">
<dtd publicId="-//Hibernate/Hibernate Mapping DTD 3.0//EN" location="${basedir}/dtd/hibernate-mapping-3.0.dtd" />
</xmlcatalog>
<style basedir="${src.dir}" destdir="${src.dir}" force="true" scanincludeddirectories="true" processor="trax" style="${basedir}/xslt/dao-gen/DaoGen.xsl" classpathref="classpath">
<factory name="org.apache.xalan.processor.TransformerFactoryImpl">
<attribute name="http://xml.apache.org/xalan/features/optimize" value="true" />
</factory>
<xmlcatalog refid="catalog" />
<mapper type="glob" from="*.hbm.xml" to="*DAO.java" />
</style>
</target>
</project>