好的,因此您一直在使用hibernate属性名称=“ hibernate.hbm2ddl.auto ” value =“ 更新 ”来不断更新数据库架构, 但是现在您需要一个完整的DDL脚本吗?
从您的Global Class 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 namefor (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的问题 。
翻译自: https://www.javacodegeeks.com/2014/10/how-to-use-hibernate-to-generate-a-ddl-script-from-your-play-framework-project.html