views:

5371

answers:

4

I'm using JPA (Hibernate's implementation) to annotate entity classes to persist to a relational database (MySQL or SQL Server). Is there an easy way to auto generate the database schema (table creation scripts) from the annotated classes?

I'm still in the prototyping phase and anticipate frequent schema changes. I would like to be able to specify and change the data model from the annotated code. Grails is similar in that it generates the database from the domain classes.

+4  A: 

You can use hbm2dll from Hibernate. The docs are here.

Vincent Ramdhanie
He asked about generating the schemas solely from the annotated classes. My team wants to do the same thing. I believe that hbm2dll only works with the .hbm.xml mapping files which my team doesn't want to use. I don't think the OP does either.
Omniwombat
+3  A: 

As a related note: Documentation for generating database schemas using EclipseLink JPA can be found here

hohonuuli
+1  A: 

Generate create and drop script for given JPA entities

We use this code to generate the drop and create statements: Just construct this class with all entity classes and call create/dropTableScript.

If needed you can use a persitence.xml and persitance unit name instead. Just say something and I post the code too.

import java.util.Collection;
import java.util.Properties;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.dialect.Dialect;
import org.hibernate.ejb.Ejb3Configuration;

/**
 * SQL Creator for Tables according to JPA/Hibernate annotations.
 *
 * Use:
 *
 * {@link #createTablesScript()} To create the table creationg script
 *
 * {@link #dropTablesScript()} to create the table destruction script
 * 
 */
public class SqlTableCreator {

    private final AnnotationConfiguration hibernateConfiguration;
    private final Properties dialectProps;

    public SqlTableCreator(final Collection> entities) {

     final Ejb3Configuration ejb3Configuration = new Ejb3Configuration();
     for (final Class entity : entities) {
      ejb3Configuration.addAnnotatedClass(entity);
     }

     dialectProps = new Properties();
     dialectProps.put("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");

     hibernateConfiguration = ejb3Configuration.getHibernateConfiguration();
    }

    /**
     * Create the SQL script to create all tables.
     * 
     * @return A {@link String} representing the SQL script.
     */
    public String createTablesScript() {
     final StringBuilder script = new StringBuilder();

     final String[] creationScript = hibernateConfiguration.generateSchemaCreationScript(Dialect
       .getDialect(dialectProps));
     for (final String string : creationScript) {
      script.append(string).append(";\n");
     }
     script.append("\ngo\n\n");

     return script.toString();
    }

    /**
     * Create the SQL script to drop all tables.
     * 
     * @return A {@link String} representing the SQL script.
     */
    public String dropTablesScript() {
     final StringBuilder script = new StringBuilder();

     final String[] creationScript = hibernateConfiguration.generateDropSchemaScript(Dialect
       .getDialect(dialectProps));
     for (final String string : creationScript) {
      script.append(string).append(";\n");
     }
     script.append("\ngo\n\n");

     return script.toString();
    }
}

KlausMeier
+1  A: 

Here's an explaination of how to use the hibernate SchemaExport class to do exactly what you want.

http://jandrewthompson.blogspot.com/2009/10/how-to-generate-ddl-scripts-from.html

Hope this helps.

Andrew Thompson