VOOZH about

URL: https://www.javacodegeeks.com/2014/10/how-to-use-hibernate-to-generate-a-ddl-script-from-your-play-framework-project.html

⇱ How to use Hibernate to generate a DDL script from your Play! Framework project


👁 Screen-Shot-2014-10-15-at-23.34.25
Ok, so you have been using the hiber­nate prop­erty name=“hibernate.hbm2ddl.auto” value=“update” to con­tin­u­ously update your data­base schema, but now you need a com­plete DDL script?

Use this method from you Global Class onStart to export the DDL scripts.  Just give it the pack­age name (with path) of your Enti­ties as well as a file name:

public void onStart(Application app) {
 exportDatabaseSchema("models", "create_tables.sql");
 }

 public void exportDatabaseSchema(String packageName, String scriptFilename) {

 final Configuration configuration = new Configuration();
 final Reflections reflections = new Reflections(packageName);
 final Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class);
 // iterate all Entity classes in the package indicated by the name
 for (final Class<?> clazz : classes) {
 configuration.addAnnotatedClass(clazz);
 }
 configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");

 SchemaExport schema = new SchemaExport(configuration);
 schema.setOutputFile(scriptFilename);
 schema.setDelimiter(";");
 schema.execute(Target.SCRIPT, SchemaExport.Type.CREATE ); // just export the create statements in the script
 }

That is it!

Thanks to @MonCalamari for answer­ing my Ques­tion on Stack­over­flow here.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

👁 Photo of Brian Porter
Brian Porter
October 17th, 2014Last Updated: October 16th, 2014
0 259 1 minute read
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz