hibernate - 具有多个数据库供应商支持的 Java/Maven/JPA/Hibernate 构建的最佳方法?

标签 hibernate maven-2 jpa build hbm2ddl

我有一个使用单个数据库的企业应用程序,但该应用程序需要支持 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 目标。运行三次似乎有点过分了...我相信每个数据库的结果几乎是相同的。

最佳答案

1) Is there a way to have this just create the schema file for each database dialect without connecting to a database?

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) If the hibernate3-maven-plug insists on actually creating the database schema, is there a way to have it drop the database and recreate it before creating the schema?

见上文。但为了以防万一,将 update 设置为 true:

<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) I am thinking that each developer (and the hudson build machine) should have their own separate database on each database server. Is this typical?

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

4) Will developers have to run Maven three times...once for each database vendor? If so, how do I merge the results on the build machine?

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

5) There is a hbm2doc goal within hibernate3-maven-plugin. It seems overkill to run this three times...I gotta believe it'd be nearly identical for each database.

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

关于hibernate - 具有多个数据库供应商支持的 Java/Maven/JPA/Hibernate 构建的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2832001/

相关文章:

java - Hibernate EntityManager 不会在持久化时抛出 EntityExistsException

maven-2 - 有没有办法使用 Maven Surefire 插件为 junit 提供 "fail fast"?

java - 用于获取集合大小的 JPA 命名查询

java - 如何将组件中的列包含在自然 ID 中?

java - 无法使用 Hibernate createQuery 实例化类

maven 2.2.1 构建错误 : Cannot find the specified class com. ibm.websphere.ssl.protocol.SSLSocketFactory

maven-2 - 在多个生命周期中运行 Maven 目标

java - Hibernate、MySQL 和名为 "Repeat"的表 - 奇怪的行为

java - 没有这样的 bean 定义并且不可能创建 bean

java - 按具有重叠路径表达式的 JPA 条件查询过滤