如何使用 maven 配置 hibernate-tools 来生成 hibernate.cfg.xml、*.hbm.xml、POJO 和 DAO
谁能告诉我如何强制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 并运行目标 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.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有一个使用
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
:正如我所说,删除您想要生成的
src/main/resources/hibernate.cfg.xml
文件。hbm2java
目标在执行自身之前调用生命周期阶段流程资源的执行(来自文档)。因此,这是默认行为,并且在hibernate3:hbm2java
或generate-sources
ifhbm2java
绑定到它时发生。I have a project that is using
hbm2cfgxml
and the<mapping resource="..."/>
entries do reflect the packagename in the path to thehbm.xml
. So there is clearly something wrong on your side. Here are a few remarks:hbm2cfgxml
on thegenerate-resources
phase, you're not generating sourcessrc/main/resources
but intarget/classses
(why do you put generated stuff in the source tree, you want aclean
to clean it).configurationfile
, notconfigurationFile
but...<configurationfile>
in the configuration ofhbm2cfgxml
? 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 thepropertyfile
property), not insrc/main/resources/hibernate.cfg.xml
(remove that file). Below a sampledatabase.properties
:And as I said, remove the
src/main/resources/hibernate.cfg.xml
file, you want to generate it.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 withhibernate3:hbm2java
orgenerate-sources
ifhbm2java
is bound to it.好的,我通过强制 maven 将
hbm.xml
文件放入 /target/classes/package/name 文件夹中解决了我的问题,所以最后我的 pom 看起来像这:并且工作正常。正如我在其他帖子中看到的那样,在某些早期构建阶段,应该从 target/hibernate3/ generated-mappings (生成它们的地方)复制这些
hbm.xml
文件默认情况下)到 target/classes/package/name (hibernate-tools 会在其中查找它们),但就我而言,它们不是(这表明我做错了什么)。因此,如果有人知道我可能做错了什么,请告诉我。否则就足够了。有一点不起作用:生成的 POJO 和 DAO 中未使用包名称:但我为此创建了另一个线程 此处。
更新:好吧,现在我终于明白了。缺少包名称的问题出现在
hbm2hbmxml
目标的配置中。我错过了其中的 componentProperties 和 packagename,因此生成的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: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 generatedhbm.xml
missed the fully classified class names. I updated the above pom, now it works fine. The issue regarding explicit copying thehbm.xml
files to the target/classes folder is still the case, though.有关如何在不使用 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.