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

标签 hibernate maven-2 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" />

所以最大的问题是:如何配置 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 并运行目标 hbm2cfgxmlhbm2hbmxml .我也不使用 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 路径中的包名。 .所以很明显你这边有问题。这里有几点说明:
  • 我会绑定(bind)hbm2cfgxmlgenerate-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/

    相关文章:

    java - Hibernate 不更新数据库(尽管它提交并读取它)

    java - 无法在 java Spring Boot 中创建新实体。获取 `` ` 无法启动嵌入式容器 ``` 错误

    maven-2 - 使用 maven 作为 Weblogic 10.3 的构建工具

    java - Eclipse 中的 Hibernate 工具 : Change in column values does not reflecting in the query result

    java - 从Hibernate Tools生成代码时,在Eclipse UI中使用NPE

    hibernate - 当表具有 UUID 字段时,我可以使用 Hibernate 工具对 Postgresql 数据库进行逆向工程吗?

    java - 与Hibernate事务代码相关的问题

    java - Maven 部署目标失败

    maven-2 - Maven2 : How do I generate a file that contains the names of the project's dependencies?

    java - 与 hibernate 的一对一关系