I'm using the packagesToScan option so that I don't have to maintain a persistence.xml.
It is possible to build a utility that would generate the ddl commands and write them to disc?
Hacking away based on a couple forum posts, I have this, but it doesn't work and also it involves some deprecated classes.
Code:
BasicConfigurator.configure();
System.setProperty("our.hibernate.hbm2ddl.auto", "none");
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/mockApplicationContext.xml");
Object fo = ctx.getBean("entityManagerFactory");
LocalContainerEntityManagerFactoryBean factory = (LocalContainerEntityManagerFactoryBean) fo;
Ejb3Configuration ejb3 = new Ejb3Configuration();
ejb3.configure(factory.getPersistenceUnitInfo(),
factory.getJpaPropertyMap());
Configuration config = ejb3.getHibernateConfiguration();
config.buildMappings();
//AuditConfiguration.getFor(config); // include audit tables
Dialect dialect = Dialect.getDialect(config.getProperties());
DatabaseMetadata metadata;
Connection conn = null;
try {
EntityManager em = factory.getObject().createEntityManager();
conn = factory.getDataSource().getConnection();
metadata = new DatabaseMetadata(conn, dialect);
String[] scripts = config.generateSchemaUpdateScript(dialect,
metadata);
// String[] scripts = config.generateSchemaCreationScript(dialect);
// String[] scripts = config.generateDropSchemaScript(dialect);
for (String script : scripts) {
System.out.println(script);
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}