我想使用 Hibernate 的模式生成来为我无法直接从我的 PC 访问的数据库生成 DDL,只能使用 hibernate 配置文件。如果可能的话,我想跳过本地 Oracle 数据库的安装。 hibernate 可以为适当的方言、版本等的“理论”数据库生成 DDL,还是这是一个白日梦?
还有其他工具可以做到这一点吗?
您可以在测试阶段使用内存数据库;
hibernate.hbm2ddl.auto =“更新”
或者您可以使用
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文件:
从 hibernate-tools 5.3 开始,您还可以使用集成的 hibernate-tools-maven-plugin。在那里您还应该找到有关如何使用它的文档。
不幸的是,带有参数/目标详细描述的 maven-site 尚未发布,但可以使用 here。
将会有一个 PR (#842) 允许生成该网站......不幸的是它还没有找到它的方式。
请尝试我的新插件,它解决了这个也困扰我多年的问题。