Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change name of Hibernate Tools reverse engineerd DAO class?

So im trying to use Hibernate Tools to reverse engineer my database and I am just getting into using Freemarker templates to weak the code it generates. The problem is I want to change the name of the DAO classes it generates. By default the DAO classes are named in the form PersonHome however to change the name to PersonDAO i modified the dao/daohome.ftl.

While this did change the generated class name to PersonDAO the java file was still called PersonHome.java.

Is there a place I can also change the generated file name to match the source code?

like image 381
3urdoch Avatar asked Aug 28 '10 10:08

3urdoch


2 Answers

Ok well I have got to the bottom of it myself. It seems while hibernate tools does support changing the filename the feature is not exposed in the Hibernate tools plugin for Eclipse which is frustrating. Instead I have had to create an ant build script to run the reverse engineering like follows.

<project name="Reverse Engineer" basedir=".">

<path id="toolslib">
 <path location="lib/hibernate3.jar" />
 <path location="lib/hibernate-tools.jar" />
 <path location="lib/freemarker.jar" />
 ...
 <path location="${jdbc.driver.jar}" />
</path>


<taskdef name="hibernatetool" 
         classname="org.hibernate.tool.ant.HibernateToolTask" 
         classpathref="toolslib" />


<hibernatetool destdir="src">
   <jdbcconfiguration 
        configurationfile="src/hibernate.cfg.xml"
        packagename="my.package.name"
        revengfile="hibernate.reveng.xml">
   </jdbcconfiguration>

   <hbmtemplate destdir="src" 
        templatepath="templates"
        template="dao/daohome.ftl"
        filepattern="{package-name}/{class-name}DAO.java">
            <property key="ejb3" value="false" />
            <property key="jdk5" value="true" />
            <property key="sessionFactoryName" value="my.HibernateSessionFactory" />
        </hbmtemplate>

    </hibernatetool>

like image 97
3urdoch Avatar answered Sep 20 '22 17:09

3urdoch


Use "Generic Exporter <hibernatetemplate>" tool instead of "DAO Code(.java)". Along with it set the following attributes in the

  1. templatename[customtemplate.ftl] for this we may use the existing daohome.ftl which is available in hibernate-tools.jar.
  2. filePattern as ${package-name}\${class-name}DAO.java

and required properties like sessionFactoryName.

like image 26
user2190077 Avatar answered Sep 22 '22 17:09

user2190077