Java 我可以使用现有的Hibernate SessionFactory重新初始化数据库架构吗?

Java 我可以使用现有的Hibernate SessionFactory重新初始化数据库架构吗?,java,hibernate,spring,Java,Hibernate,Spring,我希望提供从GUI中完全删除webapp数据库的功能,使用从AnnotationConfiguration中收集的自动生成的DDL重建表 目前,我通过编程将hibernate.hbm2ddl.auto设置为create,然后创建一个新的SessionFactory来实现这一点 AnnotationConfiguration cfg = new AnnotationConfiguration(); if (nukeDB) cfg.setProperty("hibernate.hbm2ddl

我希望提供从GUI中完全删除webapp数据库的功能,使用从AnnotationConfiguration中收集的自动生成的DDL重建表

目前,我通过编程将hibernate.hbm2ddl.auto设置为create,然后创建一个新的SessionFactory来实现这一点

AnnotationConfiguration cfg = new AnnotationConfiguration();
if (nukeDB)
    cfg.setProperty("hibernate.hbm2ddl.auto", "create");
for (Class<?> clas : HibernateMappedClasses) {
    cfg.addAnnotatedClass(clas);
}
return cfg.buildSessionFactory();
但这需要从Spring为应用程序其余部分构建的注释配置中创建一个新的注释配置


我可以使用我现有的SessionFactory重新创建数据库模式吗?

这在原始Hibernate中并不简单,但考虑到我使用的是Spring,结果证明LocalSessionFactoryBean具有方法dropDatabaseSchema和createDatabaseSchema,这两种方法共同完成了这个任务

如果没有Spring,我想我会复制这些方法中的代码