Created
August 7, 2015 09:01
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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