mmm*_*mmm 14 maven-2 hibernate hibernate-tools
任何人都可以告诉我如何强制maven在使用包路径自动生成的hibernate.cfg.xml文件中映射.hbm.xml文件之前?
我的一般想法是,我想通过maven使用hibernate-tools来为我的应用程序生成持久层.所以,我需要hibernate.cfg.xml,然后是所有my_table_names.hbm.xml,最后生成POJO.然而,hbm2java当我将*.hbm.xml文件放入src/main/resources/package/path/文件夹但hbm2cfgxml仅通过表名指定映射文件时,目标将无效,即:
<mapping resource="MyTableName.hbm.xml" />
Run Code Online (Sandbox Code Playgroud)
所以最大的问题是:我如何配置hbm2cfgxml以便hibernate.cfg.xml如下所示:
<mapping resource="package/path/MyTableName.hbm.xml" />
Run Code Online (Sandbox Code Playgroud)
我的pom.xml目前看起来像这样:
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <id>hbm2cfgxml</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>hbm2cfgxml</goal>
            </goals>
            <inherited>false</inherited>
            <configuration>
                <components>
                    <component>
                        <name>hbm2cfgxml</name>
                        <implemetation>jdbcconfiguration</implementation>
                        <outputDirectory>src/main/resources/</outputDirectory>
                    </component>
                </components>
                <componentProperties>
                    <packagename>package.path</packageName>
                    <configurationFile>src/main/resources/hibernate.cfg.xml</configurationFile>
                </componentProperties>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
然后是第二个问题:有没有办法告诉maven在执行之前将资源复制到目标文件夹hbm2java?目前我正在使用
mvn clean resources:resources generate-sources
Run Code Online (Sandbox Code Playgroud)
为此,但必须有一个更好的方法.
谢谢你的帮助.
更新:
@Pascal:谢谢你的帮助.现在,映射的路径工作正常,但我不知道之前有什么问题.在从中读取数据库配置时,写入hibernate.cfg.xml可能存在一些问题(尽管文件已更新).
我删除了文件hibernate.cfg.xml,将其替换为database.properties并运行目标hbm2cfgxml和hbm2hbmxml.我还没有使用outputDirectory,也没有configurationfile在这些目标了.
结果,文件hibernate.cfg.xml和所有文件*.hbm.xml都生成到我的target/hibernate3/generated-mappings /文件夹中,这是默认值.然后我hbm2java用以下内容更新了目标:
<componentProperties>
    <packagename>package.name</packagename>
    <configurationfile>target/hibernate3/generated-mappings/hibernate.cfg.xml</configurationfile>
</componentProperties>
Run Code Online (Sandbox Code Playgroud)
但后来我得到以下内容:
[INFO] --- hibernate3-maven-plugin:2.2:hbm2java (hbm2java) @ project.persistence ---
[INFO] using configuration task.
[INFO] Configuration XML file loaded: file:/C:/Documents%20and%20Settings/mmm/workspace/project.persistence/target/hibernate3/generated-mappings/hibernate.cfg.xml
12:15:17,484  INFO org.hibernate.cfg.Configuration - configuring from url: file:/C:/Documents%20and%20Settings/mmm/workspace/project.persistence/target/hibernate3/generated-mappings/hibernate.cfg.xml
12:15:19,046  INFO org.hibernate.cfg.Configuration - Reading mappings from resource : package.name/Messages.hbm.xml
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2java (hbm2java) on project project.persistence: Execution hbm2java of goal org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2java failed: resource: package/name/Messages.hbm.xml not found
Run Code Online (Sandbox Code Playgroud)
我该如何处理?当然我可以补充一下:
<outputDirectory>src/main/resources/package/name</outputDirectory>
Run Code Online (Sandbox Code Playgroud)
为了hbm2hbmxml目标,但我认为这不是最好的方法,或者是它?有没有办法让所有生成的代码和资源远离src/文件夹?
我假设,这种方法的目标不是生成我的src/main/java或/ resources文件夹中的任何源,而是将生成的代码保存在目标文件夹中.由于我普遍同意这一观点,我想继续执行最终执行hbm2dao和打包项目,以用作业务层中生成的持久层组件.这也是你的意思吗?
如何配置hbm2cfgxml以便hibernate.cfg.xml如下所示(...)
我有一个正在使用的项目,hbm2cfgxml并且<mapping resource="..."/>条目确实反映了路径中的packagename hbm.xml.所以你身边显然有些不对劲.以下是一些评论:
hbm2cfgxml在generate-resources阶段上绑定,你没有生成源src/main/resources但是在target/classses(为什么你把生成的东西放在源代码树中,你想要clean清理它).configurationfile不是,configurationFile但......<configurationfile>在配置中有hbm2cfgxml什么?你想在这里生成它...我会删除它.更新:您应该输入连接到数据库所需的信息src/main/resources/database.properties(这是propertyfile属性的默认值),而不是src/main/resources/hibernate.cfg.xml(删除该文件).以下示例database.properties:
hibernate.connection.driver_class=org.apache.derby.jdbc.ClientDriver
hibernate.connection.url=jdbc:derby://localhost:1527//home/pascal/Projects/derbyDBs/EMPLDB
hibernate.connection.username=APP
hibernate.connection.password=APP
hibernate.dialect=org.hibernate.dialect.DerbyDialect
Run Code Online (Sandbox Code Playgroud)
正如我所说,删除src/main/resources/hibernate.cfg.xml文件,你想生成它.
有没有办法告诉maven在执行hbm2java之前将资源复制到目标文件夹?(......)
该hbm2java目标调用之前执行自身的生命周期阶段过程资源的执行(从文档).所以这是默认行为,并发生hibernate3:hbm2java或者generate-sources 如果 hbm2java被绑定到它.
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           31313 次  |  
        
|   最近记录:  |