谁能告诉我如何强制 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
和打包项目以用作业务层生成的持久层组件。这也是你的意思吗?