我想使用 Hibernate 的模式生成为我无法直接从我的 PC 访问的数据库生成 DDL,只能使用 Hibernate 配置文件。如果可能的话,我想跳过本地 oracle 数据库的安装。 hibernate 可以为适当方言、版本等的“理论”数据库生成 DDL,还是白日梦?
是否有其他工具可以做到这一点?
最佳答案
您可以使用 In-memory database during testing phase ;
hibernate.hbm2ddl.auto="更新"
或者您可以使用
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/