Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
DDL Generation, Hibernate 4+, Java 8, Reflections, Works with @Entities + different @TypeDef annotated package
import javax.persistence.Entity;
import javax.persistence.MappedSuperclass;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.Target;
import org.junit.Test;
import org.reflections.Reflections;
public class HibernateDDLGenerator {
public static final String ENTITIES_PACKAGE = "com.something";
public static final String ANNOTATED_PACKAGE = "com";
public static final String HBM_DIALECT = "org.hibernate.dialect.Oracle10gDialect";
@Test
public void ddl() throws Exception {
new SchemaExport(createHibernateConfig())
.setOutputFile("/tmp/ddl.sql")
.setFormat(true)
.setDelimiter(";")
.create(Target.EXPORT);
}
private Configuration createHibernateConfig() {
Configuration conf = new Configuration();
final Reflections reflections = new Reflections(ENTITIES_PACKAGE);
reflections.getTypesAnnotatedWith(MappedSuperclass.class).forEach(conf::addAnnotatedClass);
reflections.getTypesAnnotatedWith(Entity.class).forEach(conf::addAnnotatedClass);
conf.addPackage(ANNOTATED_PACKAGE) // contains @TypeDefs
.setProperty(AvailableSettings.DIALECT, HBM_DIALECT);
return conf;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment