문제

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?

도움이 되었습니까?

해결책

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>

다른 팁

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.

I didn't look closely at this but I think you'll have to modify the DAONewExporter class (see HBX-343 for some inspiration).

I am adding the bits that were missing.

Use case: Modify DAO names class generated by hibernate tools

Solution: I used mvn with ant to do this.A roundabout way but its easier for those who have mvn set up.

<taskdef name="hibernatetool"
            classname="org.hibernate.tool.ant.HibernateToolTask">
    <classpath>
        <fileset dir="${basedir}/lib">
            <include name="*.jar"/>
        </fileset>
    </classpath>
</taskdef>


<target name="gen_hibernate"
            description="generate hibernate classes">
    <hibernatetool destdir="${basedir}/src/main/java">
        <jdbcconfiguration 
    configurationfile="${basedir}/hibernate.cfg.xml"
    packagename="com.bcbsmt.eie.framework.dto"
    revengfile="${basedir}/hibernate.reveng.xml">
        </jdbcconfiguration>

        <hbmtemplate destdir="${basedir}/src/main/java" 
    templatepath="${basedir}/src/main/resources/template"
    template="dao/daohome.ftl"


    filepattern="{package-name}/{class-name}DAO.java">
            <property key="ejb3" value="false" />
            <property key="jdk5" value="true" />

        </hbmtemplate>

        <hbmtemplate destdir="${basedir}/src/main/java" 
                templatepath="${basedir}/src/main/resources/template"
                template="pojo/Pojo.ftl"


                filepattern="{package-name}/{class-name}.java">
                        <property key="ejb3" value="false" />
                        <property key="jdk5" value="true" />

                    </hbmtemplate>


    </hibernatetool>
</target>

maven POM:

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.bcbsmt HibernateAnnnotationSample 0.0.1-SNAPSHOT

</dependencies>
<build>
    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <id>generate-sources</id>
                    <phase>install</phase>
                    <configuration>
                        <target>
                               <ant antfile="${basedir}/build.xml">
                                <target name="gen_hibernate" />
                            </ant>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Jars needed: These jars should be in ${basedir}/lib folder.Versions are as per your wish

asm-1.5.3.jar cglib-2.1_3.jar commons-collections-20030418.083655.jar commons-logging-1.1.1.jar dom4j-1.6.1.jar freemarker-2.3.8.jar hibernate-3.3.2.jar hibernate-annotations-3.5.6-Final.jar hibernate-tools-3.2.4.GA.jar jtidy-r8-20060801.jar log4j-1.2.17.jar slf4j-api-1.5.8.jar slf4j-log4j12-1.5.8.jar sqljdbc-4.0.jar

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top