Custom hibernate tool exporter

做~自己de王妃 提交于 2019-12-25 02:43:13

问题


I use the maven plug in to generate pojo and dao :

<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<executions>
    <execution>
        <phase>generate-sources</phase>
        <goals>
            <goal>hbm2java</goal>
            <goal>hbm2dao</goal> 
            <goal>hbm2ddl</goal>
        </goals>
    </execution>
</executions>
<configuration>
    <components>
        <component>
            <name>hbm2java</name>
            <implementation>configuration</implementation>
            <outputDirectory>src/main/java</outputDirectory>
        </component>
        <component>
            <name>hbm2dao</name>
            <implementation>configuration</implementation>
            <outputDirectory>src/main/java</outputDirectory>
        </component>
        <component>
            <name>hbm2ddl</name>
            <implementation>configuration</implementation>
            <outputDirectory>src/main/resources/sql</outputDirectory>
        </component>
    </components>
    <componentProperties>
        <jdk5>true</jdk5>
        <format>true</format>
        <configurationfile>src/main/resources/hibernate/hibernate.cfg.xml</configurationfile>

        <drop>true</drop>
        <create>true</create>
        <outputfilename>init.sql</outputfilename>
        <templatepath>src/main/resources/templateftl</templatepath>

    </componentProperties>
</configuration>

awfully the dao and pojo are generated in the same package

In hibernate tools it is hardcoded

DAOExporter :  setFilePattern("{package-name}/{class-name}Home.java");
POJOExporter : setFilePattern("{package-name}/{class-name}.java");

I find it is extremly ugly and I would like to have pojo and dao in different package and also no suffixing the Dao by "Home" but just "Dao"

Do you know if there is some way to provide a custom Exporter implementation or configure something in the pluging to achieve this ?

thanks


回答1:


Finaly I used JD-gui to decompile and read the code of the plugin and I managed to do it running 2 times the plugin with the goal hbmtemplate each times using a different template : one for the entity one for the dao.

I found out that it is possible to use a custom exporter by specifying it in exporterclass. maven plugins lack some king of xsd...

    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <id>genpojo</id>
            <goals>
                <goal>hbmtemplate</goal> 
                <goal>hbm2ddl</goal>
            </goals>
            <configuration>
            <components>
                <component>
                    <name>hbmtemplate</name>
                    <implementation>configuration</implementation>
                    <outputDirectory>src/main/java</outputDirectory>
                </component>                        
                <component>
                    <name>hbm2ddl</name>
                    <implementation>configuration</implementation>
                    <outputDirectory>src/main/resources/sql</outputDirectory>
                </component>
            </components>
            <componentProperties>
                <jdk5>true</jdk5>
                <format>true</format>
                <configurationfile>src/main/resources/hibernate/hibernate.cfg.xml</configurationfile>
                <drop>true</drop>
                <create>true</create>
                <outputfilename>init.sql</outputfilename>
                <templatepath>src/main/resources/templateftl</templatepath>
                <filepattern>{package-name}/pojogen/{class-name}.java</filepattern>
                <template>pojo/Pojo.ftl</template>
            </componentProperties>
        </configuration>
        </execution>
        <execution>
            <phase>generate-sources</phase>
            <id>gendao</id>
            <goals>                         
                <goal>hbmtemplate</goal>
            </goals>
            <configuration>
            <components>                        
                 <component>
                    <name>hbmtemplate</name>
                    <implementation>configuration</implementation>
                    <outputDirectory>src/main/java</outputDirectory>
                </component>
            </components>
            <componentProperties>
                <jdk5>true</jdk5>
                <format>true</format>
                <configurationfile>src/main/resources/hibernate/hibernate.cfg.xml</configurationfile>
                <drop>true</drop>
                <create>true</create>
                <templatepath>src/main/resources/templateftl</templatepath>
                <!--  <exporterclass>com.db.exporter.MyDAOExporter</exporterclass>-->
                <filepattern>{package-name}/daogen/{class-name}Dao.java</filepattern>
                <template>dao/daohome.ftl</template>
            </componentProperties>
        </configuration>
        </execution>
    </executions>

</plugin>


来源:https://stackoverflow.com/questions/20662640/custom-hibernate-tool-exporter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!