Reverse engineer DDL from JPA entities

隐身守侯 提交于 2019-11-28 17:56:27
andri

I don't think there is an universal way of doing this with JPA, you have to directly use the underlying JPA implementation to achieve this.

For Hibernate, there are several possibilities:

  • Use the method duffymo posted earlier, that makes Hibernate update the database schema automatically.
  • If you do not want that, you can use the hbm2ddl tool from Hibernate Tools (note: link sucks, but apparently their home page is a bit broken right now). You can use that to automatically generate database creation scripts from your JPA entities; there are also plugins for Maven and Ant that invoke hbm2ddl automatically.

For EclipseLink (formerly Oracle TopLink, the JPA 2.0 RI) see Using EclipseLink JPA Extensions for Schema Generation. In principle it is very similar to Hibernate, although at first glance I don't see anything that could be used as a stand-alone utility for creating a DB script.

Other JPA implementations (BEA/Oracle Kodo, Apache OpenJPA) probably have their own specific methods of achieving this.

Try adding the following to your persistence.xml

For Hibernate:

To create:

<property name="hibernate.hbm2ddl.auto" value="update"/>

To drop and create:

<property name="hibernate.hbm2ddl.auto" value="create-drop"/>

For Toplink:

To create:

<property name="toplink.ddl-generation" value="create-tables"/>

To drop and create:

<property name="toplink.ddl-generation" value="drop-and-create-tables"/>

For EclipseLink:

To create:

<property name="eclipselink.ddl-generation" value="create-tables"/>

To drop and create:

<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>

When I use Hibernate I simply add this to my config file:

      <property name="hbm2ddl.auto">update</property>

Takes care of everything. I'm not sure what the equivalent is for JPA, but it's so heavily influenced by Hibernate that I'd be surprised if you couldn't find something similar.

No tool needed. I usually just run a simple JUnit test and the database is created for me.

By maven plugin:

        <plugin>
            <!-- run "mvn hibernate3:hbm2ddl" to generate a schema -->
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>hibernate3-maven-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <hibernatetool>
                    <classpath>
                        <path location="${project.build.directory}/classes" />
                        <path location="${project.basedir}/src/main/resources/META-INF/" />                                               
                    </classpath>   

                    <jpaconfiguration persistenceunit="galleryPersistenceUnit" />                     
                    <hbm2ddl create="true" export="false" destdir="${project.basedir}/target" drop="true" outputfilename="mysql.sql" format="true" console="true"/>
                </hibernatetool>
            </configuration>
        </plugin>
Randy Stegbauer

I use Hibernate's org.hibernate.tool.hbm2ddl.SchemaExport class and this small method to

generate the schema in the database:

 public static void rebuildSchema()
 {
     configuration = new Configuration();
     configuration.configure();

     new SchemaExport(configuration)
         .setHaltOnError(true)
         .execute(false, true, false, false);
 }

to create the DDL in an external file, use this call to execute.

     new SchemaExport(configuration)
         .setHaltOnError(true)
         .setOutputFile(outputFile)
         .setImportFile("")
         .setDelimiter(";")
         .setFormat(true)
         .execute(false, false, false, true);

It's considered bad form to set "hibernate.hbm2ddl.auto" to "update", since automatically changing the production database may break it. For an explanation, see Hibernate hbm2ddl.auto possible values and what they do?.

DataNucleus has its own SchemaTool capable of generating the schema for you, or generating the required DDL statements for you to adapt and apply yourself.

--Andy (DataNucleus)

Adding to the list of James McMahon:

For OpenJPA:

<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>

Antonio Goncalves says in his blog about API to generate the schema.
In JPA 2.1 generateSchema method was introduced for this purpose.

Example from blog:

public class Main {
    public static void main(String[] args) {
        Persistence.generateSchema("samplePU", null);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!