Hibernate Schema生成可以用来生成没有数据库连接的DDL吗

问题描述 投票:0回答:3

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

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

java database oracle hibernate ddl
3个回答
4
投票
  1. 您可以在测试阶段使用内存数据库

    hibernate.hbm2ddl.auto =“更新”

  2. 或者您可以使用

    hibernatetool
    中的
    Maven
    生成 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 语句)

0
投票

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

不幸的是,带有参数/目标详细描述的 maven-site 尚未发布,但可以使用 here

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


0
投票

请尝试我的新插件,它解决了这个也困扰我多年的问题。

https://github.com/archiecobbs/hibernate-jpa-schemagen

© www.soinside.com 2019 - 2024. All rights reserved.