可以使用Hibernate Schema生成在没有数据库连接的情况下生成DDL吗

Ste*_*hen 5 java database oracle ddl hibernate

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

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

Vla*_*cea 4

  1. 您可以在测试阶段使用内存数据库

    hibernate.hbm2ddl.auto =“更新”

  2. hibernatetool或者您可以使用from生成 DDL Maven

    <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>
    
    Run Code Online (Sandbox Code Playgroud)

    Maven插件将生成以下 DDL 文件:

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