【问题标题】:Can Hibernate Schema generation be used to generate DDL without database connectionHibernate Schema生成可以在没有数据库连接的情况下生成DDL吗
【发布时间】:2015-09-01 18:15:09
【问题描述】:

我想使用 Hibernate 的模式生成来为我无法直接从我的 PC 访问的数据库生成 DDL,只需使用 hibernate 配置文件。如果可能,我想跳过本地 oracle 数据库的安装。 hibernate 可以为适当的方言、版本等的“理论”数据库生成 DDL,还是这是一个白日梦?

还有其他工具可以做到这一点吗?

【问题讨论】:

  • 如果你已经准备好在休眠中使用 Spring,那么 Spring 3 支持嵌入式 Java 数据库引擎。嵌入式数据库,如 HSQL、H2 或 Derby。详情View这个。
  • 听起来很酷 - 但我需要 Oracle。
  • 不安装oracle。我认为不可能。Oracle Database XE-是一个入门级、占用空间小、易于安装且免费的数据库。您可以轻松使用它。

标签: java database oracle hibernate ddl


【解决方案1】:
  1. 您可以使用In-memory database during testing phase

    hibernate.hbm2ddl.auto="update"

  2. 或者您可以使用来自Mavenhibernatetool 生成您的DDL:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
            <execution>
                <id>generate-test-sql-scripts</id>
                <phase>generate-test-resources</phase>
                <goals>
                    <goal>run</goal>
                </goals>
                <configuration>
                    <tasks>
                        <property name="maven_test_classpath" refid="maven.test.classpath"/>
                        <path id="hibernate_tools_path">
                            <pathelement path="${maven_test_classpath}"/>
                        </path>
                        <property name="hibernate_tools_classpath" refid="hibernate_tools_path"/>
                        <taskdef name="hibernatetool"
                                 classname="org.hibernate.tool.ant.HibernateToolTask"/>
                        <mkdir dir="${project.build.directory}/test-classes/hsqldb"/>
                        <hibernatetool destdir="${project.build.directory}/test-classes/hsqldb">
                            <classpath refid="hibernate_tools_path"/>
                            <jpaconfiguration persistenceunit="testPersistenceUnit"
                                              propertyfile="src/test/resources/META-INF/spring/jdbc.properties"/>
                            <hbm2ddl drop="false" create="true" export="false"
                                     outputfilename="create_db.sql"
                                     delimiter=";" format="true"/>
                            <hbm2ddl drop="true" create="false" export="false"
                                     outputfilename="drop_db.sql"
                                     delimiter=";" format="true"/>
                        </hibernatetool>
                    </tasks>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    这个Maven插件会生成以下DDL文件:

    • create_db.sql(包含用于创建数据库的所有 DDL 语句)
    • drop_db.sql(包含删除数据库的所有 DDL 语句)

【讨论】:

  • &lt;pathelement path="${maven_test_classpath}"/&gt; 是什么?
  • 这是测试类路径的路径,这样您就可以让 Ant 任务访问您的 Maven 测试阶段定义的相同库。
【解决方案2】:

从 hibernate-tools 5.3 开始,您还可以使用集成的 hibernate-tools-maven-plugin。在那里您还应该找到有关如何使用它的文档。

不幸的是,带有参数/目标详细描述的 maven 站点尚未发布,但有一些旧版本可用 here

会有一个 PR (#842) 允许生成该网站...不幸的是它还没有找到它的方式。

【讨论】:

    猜你喜欢
    • 2018-12-12
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多