How can I create a ddl for my jpa entities from java code?

前端 未结 5 940
伪装坚强ぢ
伪装坚强ぢ 2021-02-11 08:15

I look for a way how I can create a ddl for my jpa annotated entities. I prefer a pure java way for this.

If possible it would be nice to have generate the drop statemen

相关标签:
5条回答
  • 2021-02-11 08:50

    OpenJPA has support for this too. The OpenJPA mapping tool can create a script or create a ddl file. The ddl should work with other JPA implementations (although each vendor has a few quirks).

    If you're using OpenJPA as a persistence provider you can configure OpenJPA to create the tables the first time they're needed by adding the SynchronizeMappings property to persistence.xml.

    Example :

    <persistence-unit name="test"> 
        <!--
           . . .
          -->
        <properties>
            <property name="openjpa.jdbc.SynchronizeMappings"
                      value="buildSchema"/>
        </properties>
        <!--
           . . .
          -->
    </persistence-unit>
    
    0 讨论(0)
  • 2021-02-11 08:54

    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.

    0 讨论(0)
  • 2021-02-11 08:57

    Hibernate has built-in support for this. See org.hibernate.tool.hbm2ddl.SchemaExport.

    0 讨论(0)
  • 2021-02-11 09:02

    DataNucleus has SchemaTool that can be invoked from java, or from the command line. It does what you require

    --Andy (DataNucleus)

    0 讨论(0)
  • 2021-02-11 09:07

    Export data from a database as sql

    Use the liquibase opensource project

    LiquiBase is an open source (LGPL), database-independent library for tracking, managing and applying database changes. It is built on a simple premise: All database changes (structure and data) are stored in an XML-based descriptive manner and checked into source control.

    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();
        }
    }
    
    
    0 讨论(0)
提交回复
热议问题