如何使用Maven配置hibernate-tools以生成hibernate.cfg.xml,*hbm.xml,POJO和DAO

【字号: 日期:2024-04-02浏览:62作者:雯心
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解决如何使用Maven配置hibernate-tools以生成hibernate.cfg.xml,*hbm.xml,POJO和DAO?

如何配置hbm2cfgxml,以便hibernate.cfg.xml看起来像下面的(…)

我有一个使用项目hbm2cfgxml和<mappingresource='...'/>项目办反映路径的包名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.ClientDriverhibernate.connection.url=jdbc:derby://localhost:1527//home/pascal/Projects/derbyDBs/EMPLDBhibernate.connection.username=APPhibernate.connection.password=APPhibernate.dialect=org.hibernate.dialect.DerbyDialect

就像我说的,删除src/main/resources/hibernate.cfg.xml文件,您想生成它。

有没有一种方法可以告诉maven在执行hbm2java之前将资源复制到目标文件夹?(…)

的hbm2java目标 之前,执行自身调用的生命周期阶段过程资源的执行(从文档)。所以这是默认行为,并发生hibernate3:hbm2java或者generate-sources hbm2java被绑定到它。

解决方法

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

相关文章: