【问题标题】:Best approach for Java/Maven/JPA/Hibernate build with multiple database vendor support?具有多个数据库供应商支持的 Java/Maven/JPA/Hibernate 构建的最佳方法?
【发布时间】:2011-02-19 09:26:53
【问题描述】:

我有一个使用单一数据库的企业应用程序,但该应用程序需要支持 mysqloraclesql*server 作为安装选项。

为了尝试保持可移植性,我们使用 JPA 注释Hibernate 作为实现。我们还为每个用于开发的数据库提供了一个测试平台实例。

该应用程序在 Maven 中构建得很好,我已经使用过 hibernate3-maven-plugin 并且可以为给定的数据库方言自动生成 DDL。

解决此问题的最佳方法是什么,以便单个开发人员可以轻松地针对所有三个数据库进行测试,并且我们基于 Hudson 的 CI 服务器可以正确构建事物。

更具体地说:

  1. 我认为 中的 hbm2ddl 目标只会生成一个模式文件,但显然它连接到一个实时数据库并尝试创建模式。有没有办法让这个只为每个数据库方言创建架构文件而不连接到数据库?

  2. 如果 hibernate3-maven-plugin 坚持实际创建数据库架构,有没有办法让它在创建架构之前删除数据库并重新创建它?

  3. 我认为每个开发人员(和 Hudson 构建机器)都应该在每个数据库服务器上拥有自己独立的数据库。这是典型的吗?

  4. 开发人员是否必须运行 Maven 三次...为每个数据库供应商运行一次?如果是这样,我如何在构建机器上合并结果?

  5. 在 hibernate3-maven-plugin 中有一个 hbm2doc 目标。运行 3 次似乎有点矫枉过正……我相信每个数据库的结果几乎相同。

【问题讨论】:

    标签: hibernate3-maven-plugin hibernate maven-2 jpa build hbm2ddl


    【解决方案1】:

    1) 有没有办法只为每个数据库方言创建架构文件而不连接到数据库?

    export 设置为false。像这样的:

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>hibernate3-maven-plugin</artifactId>
      <version>2.2</version>
      <configuration>
        <components>
          <component>
            <name>hbm2ddl</name>
            <implementation>annotationconfiguration</implementation>
          </component>
        </components>
        <componentProperties>
          <export>false</export><!-- do not export to the database -->
          <drop>true</drop>
          <configurationfile>src/main/resources/hibernate.cfg.xml</configurationfile>
          <outputfilename>my_schema.ddl</outputfilename>
        </componentProperties>
      </configuration>
    </plugin>
    

    2) 如果 hibernate3-maven-plug 坚持实际创建数据库模式,有没有办法让它在创建模式之前删除数据库并重新创建它?

    见上文。但以防万一,对于这组updatetrue

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>hibernate3-maven-plugin</artifactId>
      <version>2.2</version>
      <configuration>
        <components>
          <component>
            <name>hbm2ddl</name>
            <implementation>annotationconfiguration</implementation>
          </component>
        </components>
        <componentProperties>
          <export>true</export>
          <update>true</update><!-- update the schema -->
          <drop>true</drop>
          <configurationfile>src/main/resources/hibernate.cfg.xml</configurationfile>
          <outputfilename>my_schema.ddl</outputfilename>
        </componentProperties>
      </configuration>
      <dependencies>
        <dependency>
          <groupId>org.apache.derby</groupId>
          <artifactId>derbyclient</artifactId>
          <version>10.5.3.0_1</version>
        </dependency>
      </dependencies>
    </plugin>
    

    3) 我认为每个开发人员(和 hudson 构建机器)都应该在每个数据库服务器上拥有自己独立的数据库。这是典型的吗?

    是的,我认为这是最佳做法(请参阅 Use one database instance per developer)。

    4) 开发人员是否必须运行 Maven 三次……每个数据库供应商一次?如果是这样,我如何在构建机器上合并结果?

    是的,很可能,我会为每个数据库使用配置文件。在构建机器上,我会build a matrix project

    5) hibernate3-maven-plugin 中有一个 hbm2doc 目标。运行 3 次似乎有点矫枉过正……我相信每个数据库的结果几乎相同。

    我不习惯这个工具,但我猜输出可能会有一些小的变化(例如,使用主键生成)。我会为每个数据库生成架构文档,但仅在发布时(当然不需要在每次构建时运行)。

    【讨论】:

    • 非常感谢。 export=false 设置成功了。 hibernate3-maven-plugin 文档太差了,这有点令人沮丧。还要感谢有关 Hudson 构建矩阵的提示,它看起来就像医生订购的那样。
    • @HDave 是的,文档并不是它最大的优势(遗憾的是,与相应的 ant 任务存在一些小差异)。
    猜你喜欢
    • 2012-11-01
    • 1970-01-01
    • 2021-02-27
    • 2012-09-19
    • 2022-11-10
    • 2017-09-25
    • 2015-06-06
    • 2017-03-14
    • 1970-01-01
    相关资源
    最近更新 更多