java - Hibernate Schema generation 是否可以在没有数据库连接的情况下生成DDL

标签 java database oracle hibernate ddl

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

是否有其他工具可以做到这一点?

最佳答案

  1. 您可以使用 In-memory database during testing phase ;

    hibernate.hbm2ddl.auto="更新"

  2. 或者您可以使用 Maven 中的 hibernatetool 生成您的 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 语句)

关于java - Hibernate Schema generation 是否可以在没有数据库连接的情况下生成DDL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30875863/

相关文章:

java - 将数据源作为字段传递时,子报表不显示所有记录

java - 使用 JavaScript 的普通 JSF 应用程序?

java - 如何使用插入语句生成 liquibase 变更日志?

C# Winforms : Mysql Database export to Excel error

java - JDBC-ORA-01861 : literal does not match format string 01861

java - 使用 Java 实现枚举工资单

java - 2个模型之间的activejdbc关联

php - 实现 "soft"保存或修改工作流的最佳方式是什么?

无法在 Oracle Pro*C 中测试空值

c# - Oracle.DataAccess.dll 抛出 NullReferenceException