【发布时间】:2011-02-20 02:22:28
【问题描述】:
谁能告诉我如何强制 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" />
所以最大的问题是:我如何配置hbm2cfgxml 以便hibernate.cfg.xml 如下所示:
<mapping resource="package/path/MyTableName.hbm.xml" />
我的 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>
然后是第二个问题:有没有办法告诉maven在执行hbm2java之前将资源复制到目标文件夹?目前我正在使用
mvn clean resources:resources generate-sources
为此,但必须有更好的方法。
感谢您的帮助。
更新:
@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>
然后我得到以下信息:
[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
我该如何处理?当然可以补充:
<outputDirectory>src/main/resources/package/name</outputDirectory>
到hbm2hbmxml 目标,但我认为这不是最好的方法,不是吗?有没有办法让所有生成的代码和资源远离src/ 文件夹?
我假设,这种方法的目标不是在我的 src/main/java 或 /resources 文件夹中生成任何源代码,而是将生成的代码保存在目标文件夹中。正如我普遍同意这个观点一样,我想继续最终执行hbm2dao 并打包项目以用作从业务层生成的持久层组件。这也是你的意思吗?
【问题讨论】:
标签: hibernate maven-2 hibernate-tools