如何使用Hibernate从Play生成DDL脚本! 框架项目
生活随笔
收集整理的這篇文章主要介紹了
如何使用Hibernate从Play生成DDL脚本! 框架项目
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
好的,因此您一直在使用hibernate屬性名稱=“ hibernate.hbm2ddl.auto ” value =“ 更新 ”來不斷更新數(shù)據(jù)庫架構(gòu), 但是現(xiàn)在您需要一個完整的DDL腳本嗎?
從您的Global Class onStart中使用此方法來導(dǎo)出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在這里回答我有關(guān)Stackoverflow的問題 。
翻譯自: https://www.javacodegeeks.com/2014/10/how-to-use-hibernate-to-generate-a-ddl-script-from-your-play-framework-project.html
總結(jié)
以上是生活随笔為你收集整理的如何使用Hibernate从Play生成DDL脚本! 框架项目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 闽侯商品房备案中心(闽候商品房网上备案)
- 下一篇: Mockito 101