I’d like to provide the ability to completely wipe my webapp’s database from its GUI, rebuilding the tables with the auto-generated DDL gleaned from an AnnotationConfiguration.
At the moment I do this by programatically setting hibernate.hbm2ddl.auto to create and then creating a new SessionFactory.
AnnotationConfiguration cfg = new AnnotationConfiguration();
if (nukeDB)
cfg.setProperty("hibernate.hbm2ddl.auto", "create");
for (Class<?> clas : HibernateMappedClasses) {
cfg.addAnnotatedClass(clas);
}
return cfg.buildSessionFactory();
but this requires creating a fresh AnnotationConfiguration from the one that Spring is building for the rest of the app.
Can I use my existing SessionFactory to recreate the database schema?
This isn’t simple in raw Hibernate, but given that I’m using Spring, it turns out that the
LocalSessionFactoryBeanhas methodsdropDatabaseSchemaandcreateDatabaseSchemathat together do the trick.Without Spring I think I’d copy the code in those methods!