谁能告诉我如何强制 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
为此,但必须有更好的方法。
谢谢你的帮助。
更新:
@帕斯卡:感谢您的帮助。映射路径现在工作正常,但我不知道以前出了什么问题。在从中读取数据库配置时写入 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
并打包项目以用作从业务层生成的持久层组件。这也是你的意思吗?
最佳答案
how can I configure hbm2cfgxml so that hibernate.cfg.xml looks like below (...)
我有一个项目正在使用
hbm2cfgxml
和 <mapping resource="..."/>
条目确实反射(reflect)了 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
正如我所说,删除
src/main/resources/hibernate.cfg.xml
文件,你想生成它。is there a way to tell maven to copy resources to the target folder before executing hbm2java? (...)
hbm2java
目标在执行自身之前调用生命周期阶段流程资源的执行(来自文档)。所以这是默认行为,发生在 hibernate3:hbm2java
上。或 generate-sources
如果 hbm2java
是必然的。
关于hibernate - 如何使用 maven 配置 hibernate-tools 以生成 hibernate.cfg.xml、*.hbm.xml、POJO 和 DAO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2843949/