14

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

4

3 回答 3

9

如何配置 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

于 2010-05-16T14:23:27.177 回答
2

好的,我通过强制 maven 将hbm.xml文件放入/target/classes/package/name文件夹来解决我的问题,所以最后我的 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文件应该从target/hibernate3/generated-mappings(默认生成它们)复制到target/classes/package/name(hibernate-tools 看起来对他们来说),但在我的情况下他们不是(这表明我做错了什么)。因此,如果有人知道我做错了什么,请告诉我。否则它就足够了。

有一件事是行不通的:包名没有在生成的 POJO 和 DAO 中使用:但我在这里为此创建了另一个线程。

更新:好的,现在我终于明白了。缺少包名称的问题在于hbm2hbmxml目标的配置。我在那里错过了带有packagename的componentProperties,因此生成的错过了完全分类的类名。我更新了上面的pom,现在可以正常工作了。但是,关于将文件显式复制到target/classes文件夹的问题仍然存在。hbm.xmlhbm.xml

于 2010-05-17T13:39:30.863 回答
1

有关如何在不使用 hibernate3-maven-plugin 的情况下将 Hibernate 工具与 maven 一起使用的示例,请查看这篇文章。这个想法是使用 Maven 运行 Hibernate 工具 Ant 任务。这种方法使您可以完全控制该过程。

于 2016-02-13T10:25:22.500 回答