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

StackOverflow https://stackoverflow.com/questions/2843949

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

有帮助吗?

解决方案

我如何配置 hbm2cfgxml 以便 hibernate.cfg.xml 如下所示(...)

我有一个正在使用的项目 hbm2cfgxml<mapping resource="..."/> 条目确实反映了路径中的包名称 hbm.xml. 。所以你这边显然有问题。以下是几点说明:

  • 我会绑定 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 文件,您想要生成它。

有没有办法告诉maven在执行hbm2java之前将资源复制到目标文件夹?(...)

hbm2java 目标 在执行自身之前调用生命周期阶段流程资源的执行 (来自文档)。这是默认行为,并且发生在 hibernate3:hbm2java 或者 generate-sources 如果 hbm2java 与它绑定。

其他提示

好的,我通过强制 Maven 将 hbm.xml 文件到 /目标/类/包/名称 文件夹,所以最后我的 pom 看起来像这样:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>hibernate3-maven-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <id>hbm2cfgxml</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>hbm2cfgxml</goal>
                    </goals>
                    <inherited>false</inherited>
                    <configuration>
                        <components>
                            <component>
                                <name>hbm2cfgxml</name>
                                <implementation>jdbcconfiguration</implementation>
                            </component>
                        </components>
                        <componentProperties>
                            <packagename>package.name</packagename>
                        </componentProperties>
                    </configuration>
                </execution>
                <execution>
                    <id>hbm2hbmxml</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>hbm2hbmxml</goal>
                    </goals>
                    <inherited>false</inherited>
                    <configuration>
                        <components>
                            <component>
                                <name>hbm2hbmxml</name>
                                <outputDirectory>target/classes</outputDirectory>
                            </component>
                        </components>
                        <componentProperties>
                            <packagename>package.name</packagename>
                        </componentProperties>
                    </configuration>
                </execution>
                <execution>
                    <id>hbm2java</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>hbm2java</goal>
                    </goals>
                    <inherited>false</inherited>
                    <configuration>
                        <components>
                            <component>
                                <name>hbm2java</name>
                                <implementation>configuration</implementation>
                            </component>
                        </components>
                        <componentProperties>
                            <packagename>package.name</packagename>
                            <configurationfile>target/hibernate3/generated-mappings/hibernate.cfg.xml</configurationfile>
                        </componentProperties>
                    </configuration>
                </execution>
                <execution>
                    <id>hbm2dao</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>hbm2dao</goal>
                    </goals>
                    <inherited>false</inherited>
                    <configuration>
                        <components>
                            <component>
                                <name>hbm2dao</name>
                                <implementation>configuration</implementation>
                            </component>
                        </components>
                        <componentProperties>
                            <packagename>package.name</packagename>
                            <configurationfile>target/hibernate3/generated-mappings/hibernate.cfg.xml</configurationfile>
                        </componentProperties>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>postgresql</groupId>
                    <artifactId>postgresql</artifactId>
                    <version>8.4-701.jdbc3</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

而且工作正常。据我在其他帖子中看到的,在一些早期构建阶段,这些 hbm.xml 文件应该从复制 目标/hibernate3/生成的映射 (默认生成它们) 目标/类/包/名称 (其中 hibernate-tools 寻找它们),但就我而言,它们不是(这表明我做错了什么)。因此,如果有人知道我可能做错了什么,请告诉我。否则就足够了。

有一点不起作用:生成的 POJO 和 DAO 中不使用包名称:但我为此创建了另一个线程 这里.

更新: 好吧,现在我终于明白了。缺少包名称的问题在于 hbm2hbmxml 目标的配置。我错过了 组件属性包裹名字 在那里,所以生成的 hbm.xml 错过了完全分类的类名。我更新了上面的pom,现在工作正常了。关于显式复制的问题 hbm.xml 文件到 目标/类别 不过,文件夹仍然如此。

有关如何将 Hibernate 工具与 Maven 结合使用的示例,而不使用 hibernate3-maven-plugin 检查 这个帖子. 。这个想法是使用 Maven 运行 Hibernate 工具 Ant 任务。这种方法使您可以完全控制该过程。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top