自定义休眠工具导出器 [英] Custom hibernate tool exporter

查看:124
本文介绍了自定义休眠工具导出器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Maven插件生成pojo和dao:

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>

很高兴dao和pojo是在同一包中生成的

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");

我发现它非常丑陋,我想将pojo和dao放在不同的包装中,也不要在"Dao"后面加上"Home",而只是在"Dao"后缀

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"

您知道是否可以通过某种方式提供自定义的Exporter实现或在插件中进行配置以实现此目的?

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

谢谢

推荐答案

最后,我使用JD-gui进行反编译并读取了插件的代码,并设法通过目标两次使用目标hbmtemplate来运行插件两次一种不同的模板:一种用于实体,一种用于dao.

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.

我发现可以通过在exporterclass中指定自定义导出程序来使用它. Maven插件缺少xsd之王...

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>

这篇关于自定义休眠工具导出器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆