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

发布于 2024-09-01 09:12:52 字数 4303 浏览 10 评论 0原文

谁能告诉我如何强制maven在自动生成的hibernate.cfg.xml文件中使用包路径映射.hbm.xml文件?

我的总体想法是,我想通过 maven 使用 hibernate-tools 为我的应用程序生成持久层。因此,我需要 hibernate.cfg.xml,然后是所有 my_table_names.hbm.xml,最后生成 POJO。然而,当我将 *.hbm.xml 文件放入 src/main/resources/package/path/ 文件夹中时,hbm2java 目标将不起作用,但 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。我也不再在这些目标中使用 outputDirectoryconfigurationfile

结果,文件 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 并将项目打包以用作从业务层生成的持久层组件。这也是你的意思吗?

can any one tell me how to force maven to precede mapping .hbm.xml files in the automatically generated hibernate.cfg.xml file with package path?

My general idea is, I'd like to use hibernate-tools via maven to generate the persistence layer for my application. So, I need the hibernate.cfg.xml, then all my_table_names.hbm.xml and at the end the POJO's generated. Yet, the hbm2java goal won't work as I put *.hbm.xml files into the src/main/resources/package/path/ folder but hbm2cfgxml specifies the mapping files only by table name, i.e.:

<mapping resource="MyTableName.hbm.xml" />

So the big question is: how can I configure hbm2cfgxml so that hibernate.cfg.xml looks like below:

<mapping resource="package/path/MyTableName.hbm.xml" />

My pom.xml looks like this at the moment:

<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>

And then the second question: is there a way to tell maven to copy resources to the target folder before executing hbm2java? At the moment I'm using

mvn clean resources:resources generate-sources

for that, but there must be a better way.

Thanks for any help.

Update:

@Pascal: Thank you for your help. The path to mappings works fine now, I don't know what was wrong before, though. Maybe there is some issue with writing to hibernate.cfg.xml while reading database config from it (though the file gets updated).

I've deleted the file hibernate.cfg.xml, replaced it with database.properties and run the goals hbm2cfgxml and hbm2hbmxml. I also don't use the outputDirectory nor configurationfile in those goals anymore.

As a result the files hibernate.cfg.xml and all *.hbm.xml are being generated into my target/hibernate3/generated-mappings/ folder, which is the default value. Then I updated the hbm2java goal with the following:

<componentProperties>
    <packagename>package.name</packagename>
    <configurationfile>target/hibernate3/generated-mappings/hibernate.cfg.xml</configurationfile>
</componentProperties>

But then I get the following:

[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

How do I deal with that? Of course I could add:

<outputDirectory>src/main/resources/package/name</outputDirectory>

to the hbm2hbmxml goal, but I think this is not the best approach, or is it? Is there a way to keep all the generated code and resources away from the src/ folder?

I assume, the goal of this approach is not to generate any sources into my src/main/java or /resources folder, but to keep the generated code in the target folder. As I generally agree with this point of view, I'd like to continue with that eventually executing hbm2dao and packaging the project to be used as a generated persistence layer component from the business layer. Is this also what you meant?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

音盲 2024-09-08 09:12:52

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

我有一个使用 hbm2cfgxml 条目确实反映了 hbm.xml 路径中的包名称。所以你这边显然有问题。这里有一些注释:

  • 我会在 generate-resources 阶段绑定 hbm2cfgxml,您不会生成源文件
  • 我不会在 src/ 中生成文件main/resources 但在 target/classses 中(为什么你把生成的东西放在源代码树中,你需要一个 clean 来清理它)。
  • 有一个拼写错误,它是 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:hbm2javagenerate-sources if hbm2java 绑定到它时发生。

how can I configure hbm2cfgxml so that hibernate.cfg.xml looks like below (...)

I have a project that is using hbm2cfgxml and the <mapping resource="..."/> entries do reflect the packagename in the path to the hbm.xml. So there is clearly something wrong on your side. Here are a few remarks:

  • I would bind hbm2cfgxml on the generate-resources phase, you're not generating sources
  • I wouldn't generate the file in src/main/resources but in target/classses (why do you put generated stuff in the source tree, you want a clean to clean it).
  • There is a typo, it's configurationfile, not configurationFile but...
  • Why the hell do you have a <configurationfile> in the configuration of hbm2cfgxml? You want to generate it here... I would remove it.

Update: You should put the informations required to connect to the database in src/main/resources/database.properties (that's the default value of the propertyfile property), not in src/main/resources/hibernate.cfg.xml (remove that file). Below a sample 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

And as I said, remove the src/main/resources/hibernate.cfg.xml file, you want to generate it.

is there a way to tell maven to copy resources to the target folder before executing hbm2java? (...)

The hbm2java goal Invokes the execution of the lifecycle phase process-resources prior to executing itself (from the documentation). So that's the default behavior and occurs with hibernate3:hbm2java or generate-sources if hbm2java is bound to it.

一城柳絮吹成雪 2024-09-08 09:12:52

好的,我通过强制 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>

并且工作正常。正如我在其他帖子中看到的那样,在某些早期构建阶段,应该从 target/hibernate3/ generated-mappings (生成它们的地方)复制这些 hbm.xml 文件默认情况下)到 target/classes/package/name (hibernate-tools 会在其中查找它们),但就我而言,它们不是(这表明我做错了什么)。因此,如果有人知道我可能做错了什么,请告诉我。否则就足够了。

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

更新:好吧,现在我终于明白了。缺少包名称的问题出现在 hbm2hbmxml 目标的配置中。我错过了其中的 componentPropertiespackagename,因此生成的 hbm.xml 错过了完全分类的类名。我更新了上面的pom,现在工作正常了。不过,将 hbm.xml 文件显式复制到 target/classes 文件夹的问题仍然存在。

Ok, I fixed my problem by forcing maven to put the hbm.xml files into the /target/classes/package/name folder, so at the end my pom looks like this:

<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>

And it works ok. As fas as I could see in other posts, in some early build phases those hbm.xml files should be copied from target/hibernate3/generated-mappings (where they are generated by default) to target/classes/package/name (where hibernate-tools looks for them), but in my case they aren't (which indicates I'm doing something wrong). So if there is anyone out there knowing what it might be I'm doing wrong, please tell me. Otherwise It'll have to suffice.

There is one thing that isn't working: the package names aren't used in the generated POJOs and DAOs: but I created another thread for this here.

Update: ok, now I finally got it. The problem with missing package names was in the hbm2hbmxml goal's configuration. I missed the componentProperties with packagename there, so the generated hbm.xml missed the fully classified class names. I updated the above pom, now it works fine. The issue regarding explicit copying the hbm.xml files to the target/classes folder is still the case, though.

肩上的翅膀 2024-09-08 09:12:52

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

For an example about how to use Hibernate tools with maven, without using hibernate3-maven-plugin check this post. The idea is to run Hibernate tools Ant task with Maven. This approach gives you the full control over the process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文