Skip to content

Instantly share code, notes, and snippets.

@bric3
Created August 7, 2015 09:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bric3/370654ecad0eb81aca22 to your computer and use it in GitHub Desktop.
Save bric3/370654ecad0eb81aca22 to your computer and use it in GitHub Desktop.
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