如何用maven配置hibernate-tools来生成hibernate.cfg.xml,* .hbm.xml,POJO和DAO [英] How to configure hibernate-tools with maven to generate hibernate.cfg.xml, *.hbm.xml, POJOs and DAOs

查看:122
本文介绍了如何用maven配置hibernate-tools来生成hibernate.cfg.xml,* .hbm.xml,POJO和DAO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我如何强制maven在自动生成的带有包路径的hibernate.cfg.xml文件中映射.hbm.xml文件之前?



My一般的想法是,我想通过maven使用hibernate-tools为我的应用程序生成持久层。所以,我需要hibernate.cfg.xml,然后是所有my_table_names.hbm.xml,最后生成POJO。然而,当我将* .hbm.xml文件放入 src / main / resources / package / path / <目录>时, hbm2java / code>文件夹,但 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>
<执行次数>
<执行>
< id> hbm2cfgxml< / id>
<阶段> generate-sources< / phase>
<目标>
< goal> hbm2cfgxml< / goal>
< /目标>
<继承>假< /继承>
<配置>
<组件>
< component>
<名称> hbm2cfgxml< / name>
<实施> jdbc配置< / implementation>
< outputDirectory> src / main / resources /< / outputDirectory>
< / component>
< /组件>
< componentProperties>
< packagename> package.path< / packageName>
< configurationFile> src / main / resources / hibernate.cfg.xml< / configurationFile>
< / componentProperties>
< / configuration>
< /执行>
< /执行次数>
< / plugin>

然后第二个问题:是否有办法告诉maven在将资源复制到目标文件夹之前执行 hbm2java ?目前,我正在使用

  mvn clean资源:resources generate-sources 

为此,但必须有更好的方法。



感谢您的帮助。 / p>

更新:
$ b

@Pascal:您的帮助。现在,映射路径正常运行,但我不知道之前出了什么问题。也许在从它读取数据库配置时写入hibernate.cfg.xml存在一些问题(尽管文件被更新)。



我已经删除了hibernate文件。 cfg.xml,将其替换为database.properties并运行目标 hbm2cfgxml hbm2hbmxml 。我也不会在这些目标中使用 outputDirectory 配置文件



因此,文件 hibernate.cfg.xml 和所有 *。hbm.xml 都是被生成到我的目标/ hibernate3 / generated-mappings /文件夹中,这是默认值。然后我更新了 hbm2java 目标:

 < componentProperties> ; 
<包裹名称>包裹名称< /包裹名称>
< configurationfile> target / hibernate3 / generated-mappings / hibernate.cfg.xml< / configurationfile>
< / componentProperties>

但是,我得到以下内容:

  [INFO] --- hibernate3-maven-plugin:2.2:hbm2java(hbm2java)@project.persistence --- 
[INFO]使用配置任务。
[INFO]配置加载的XML文件: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 - 从url进行配置:file:/ C:/Documents%20and%20Settings/mmm/workspace/project.persistence/target/hibernate3/generated-mappings/hibernate.cfg.xml
12:15:19,046信息org.hibernate.cfg.Configuration - 从资源读取映射:package.name/Messages.hbm.xml
[INFO] ----------- -------------------------------------------------- -----------
[INFO] BUILD FAILURE
[INFO] ----------------------- -------------------------------------------------
[错误]未能执行目标org.codehaus.mojo:hibernate3-maven-plugin:2.2:项目中的hbm2java(hbm2java)project.persistence:执行目标org.codehaus.mojo的hbm2java:hibernate3-maven-plugin: 2.2:hbm2java failed:resource:package / name / Messages.hbm.xml not found

Ho我该怎么处理?当然我可以添加:

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

hbm2hbmxml 目标,但我认为这不是最好的方法,还是它?是否有一种方法可以将生成的代码和资源从 src / 文件夹中保留 all

我假设,这种方法的目标不是为我的src / main / java或/ resources文件夹生成任何源文件,而是将生成的代码保留在目标文件夹中。正如我一般同意这种观点,我想继续执行,最终执行 hbm2dao 并打包项目以用作业务中生成的持久层组件层。这也是你的意思吗?

解决方案


如何配置hbm2cfgxml以便hibernate.cfg。 xml看起来像下面(...)


我有一个项目使用 hbm2cfgxml < mapping resource =.../> 条目确实反映了 hbm.xml路径中的包名。所以你的方面显然有问题。以下是一些评论:


  • 我会在<$ c上绑定 hbm2cfgxml $ c> generate-resources 阶段,您不生成源文件

  • 我不会在 src / main /资源但是在 target / classses 中(为什么你要把生成的东西放在源代码树中,你想要一个
  • 有一个错字,它是 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 goal 调用生命周期阶段过程资源执行自己(从文档)。所以这是默认行为,并且在 hibernate3:hbm2java generate-sources if hbm2java 被绑定到它。


    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?

    解决方案

    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.

    这篇关于如何用maven配置hibernate-tools来生成hibernate.cfg.xml,* .hbm.xml,POJO和DAO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆