2009-06-30 9 views
1

私はjpaアノテートされたエンティティのためにどのようにddlを作成することができるかを探します。 私はこのために純粋なJavaの方法を好む。Javaコードからjpaエンティティのddlを作成するにはどうすればよいですか?

可能であれば、drop文も生成しておくといいでしょう。

+0

これはhttp://stackoverflow.com/questions/297438/auto-generate-data-schema-from-jpa-annotated-entity-classesの繰り返しです。 – stevedbrown

答えて

2

SQLなどのデータベースからエクスポートデータ

使用liquibaseオープンソースプロジェクト

LiquiBaseを、追跡、管理、データベースの変更を適用するためのオープンソース(LGPL)、データベースに依存しないライブラリです。これは簡単な前提で構築されています。すべてのデータベースの変更(構造とデータ)はXMLベースの記述方法で保存され、ソース管理にチェックインされます。 ただ、すべてのエンティティクラスで、このクラスを構築し、/ dropTableScriptを作成して呼び出す:

は、私たちがドロップを生成し、文を作成するには、このコードを使用し

与えられたJPAエンティティ用のスクリプトを作成し、ドロップ生成します。

必要に応じて、代わりにpersitence.xmlとpersitance unitの名前を使用できます。ちょうど何かを言う と私もコードを投稿します。

 
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

答えをいただきありがとうございます、persistence.xmlを使用できますか? –

2

Hibernateにはこれが組み込まれています。 org.hibernate.tool.hbm2ddl.SchemaExportを参照してください。

2

OpenJPAもこれをサポートしています。 OpenJPA mapping toolは、スクリプトを作成したり、ddlファイルを作成することができます。 ddlは他のJPAの実装と連携する必要があります(各ベンダーにはいくつかの特徴があります)。

OpenJPAを永続性プロバイダとして使用している場合は、SynchronizeMappingsプロパティをpersistence.xmlに追加することで、必要なときに初めて表を作成するようにOpenJPAを構成できます。

例:

<persistence-unit name="test"> 
    <!-- 
     . . . 
     --> 
    <properties> 
     <property name="openjpa.jdbc.SynchronizeMappings" 
        value="buildSchema"/> 
    </properties> 
    <!-- 
     . . . 
     --> 
</persistence-unit> 
3

DataNucleusのは、javaから、またはコマンドラインから起動することができSchemaToolを持っています。それはあなたが

--Andy(DataNucleus

関連する問題