ddl语句
好的,因此您一直在使用hibernate属性名称=“ hibernate.hbm2ddl.auto ” value =“更新”来不断更新数据库架构,但是现在您需要一个完整的DDL脚本吗?从全局类onStart中使用此方法可导出DDL脚本。 只需为其提供实体的包名称(带有路径)以及文件名即可:
public void onStart(Application app) {
exportDatabaseSchema("models", "create_tables.sql");
}
public void exportDatabaseSchema(String packageName, String scriptFilename) {
final Configuration configuration = new Configuration();
final Reflections reflections = new Reflections(packageName);
final Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class);
// iterate all Entity classes in the package indicated by the name
for (final Class<?> clazz : classes) {
configuration.addAnnotatedClass(clazz);
}
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");
SchemaExport schema = new SchemaExport(configuration);
schema.setOutputFile(scriptFilename);
schema.setDelimiter(";");
schema.execute(Target.SCRIPT, SchemaExport.Type.CREATE ); // just export the create statements in the script
}
这就对了!
感谢@MonCalamari在这里回答我有关Stackoverflow的问题。
ddl语句