2013-01-04 15 views
5

私の前の質問へのフォローアップの質問:Generate an SQL DB creation script with Hibernate 4休止状態はSchemaExportと永続ユニット

目標は、与えられた永続ユニットのSQLスキーマを使用してファイルを生成することができ、コマンドラインツールを持つことである(同様hibernatetool-へhibernateツールにはhbm2ddl Antタスクが存在します)。

私の前の質問に対する回答によれば、org.hibernate.tool.hbm2ddl.SchemaExportでこれを達成できます。

Configurationにすべてのエンティティを追加する代わりに(以前の回答で提案されたとおり)PersistenceUnitと指定したいと思います。

を追加する永続ユニットを休止状態にするConfiguration

何かコメントサンプルpersistence.xmlに要求されるように編集

Properties properties = new Properties(); 
properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect"); 
... 
EntityManagerFactory entityManagerFactory = 
    Persistence.createEntityManagerFactory("persistentUnitName", properties); 
Configuration configuration = new Configuration(); 

... missing part ... 

SchemaExport schemaExport = new SchemaExport(configuration); 
schemaExport.setOutputFile("schema.sql"); 
... 

のような。あなたのクラスは、XMLマッピング(hbm S)を経由してマッピングされている場合は各クラスは、@Entity

<persistence 
    xmlns="http://java.sun.com/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
    version="1.0" 
> 

    <persistence-unit 
     name="doiPersistenceUnit" 
     transaction-type="JTA" 
    > 

     <provider>org.hibernate.ejb.HibernatePersistence</provider> 
     <jta-data-source>jdbc/doi</jta-data-source> 


     <class>ch.ethz.id.wai.doi.bo.Doi</class> 
     [...] 
     <class>ch.ethz.id.wai.doi.bo.DoiPool</class> 

     <exclude-unlisted-classes>true</exclude-unlisted-classes> 

     <properties> 
      <property name="hibernate.show_sql"      value="false" /> 
      <property name="hibernate.format_sql"     value="false" /> 
      <property name="hibernate.connection.characterEncoding" value="utf8" /> 
      <property name="hibernate.connection.charSet"   value="utf8" /> 
     </properties> 

    </persistence-unit> 

</persistence> 
+0

あなたは 'config.addAnnotatedClass(MyMappedPojo1.class);'行を保存しますか? – yair

+0

@yairはいすべてのクラスを手動で指定しないようにしたいと思います(ハードコードを避けてください)。私はpersistence.xmlファイルを解析できることは知っていますが、簡単な方法があると思われます。 – Matteo

+0

私は、あなたが構成に方言を渡していなくても、SchemaExportが作成されると失敗すると思います。 – kboom

答えて

6

まあでアノテートされた - あなたはまっすぐconfig.addJar(myJarFile)config.add(myXmlFile)を使用してConfigurationインスタンスにXMLSが含まdocumnetsまたはjarファイルを追加することができます。あなたは注釈付きクラスをスキャンする場合は

しかし、 - 私は(addPackageは、メタデータとないクラスが追加されます)にHibernateていない、そのような単純なオプションを知っています。

あなたは(クラスをORMので、おそらくいくつかの時間を節約するか、おそらくあなたを含むためにあなたが知っている特定のパッケージごとにそれを行う)独自のスキャンロジックを実装し、config.addAnnotatedClass(myAnnotatedClass)ですべての注釈付きのクラスを追加することができます。

UPDATE 2

ああ、さらに良い、あなただけgetManagedTypes()を経由して、持続性ユニットのManagedType Sを繰り返すことができます。

EntityManagerFactory entityManagerFactory = 
    Persistence.createEntityManagerFactory(unitName, config.getProperties()); 
final Set<ManagedType<?>> managedTypes = 
    entityManagerFactory.getMetamodel().getManagedTypes(); 
    for (ManagedType<?> managedType : managedTypes) { 
    final Class<?> javaType = managedType.getJavaType(); 
    config.addAnnotatedClass(javaType); 
} 

UPDATE

あなたはそれぞれのPersistenceUnitを決定することができますEntity - xmlを解析しない - 関連EntityManagerFactoryと照合することにより:

Class aClass = ... // get the class from your scanning 
EntityManagerFactory entityManagerFactory = 
    Persistence.createEntityManagerFactory(unitName, config.getProperties()); 
ManagedType<?> managedType = null; 
try { 
    managedType = entityManagerFactory.getMetamodel().managedType(aClass); 
} catch (IllegalArgumentException e) { 
    // happens when aClass isn't a type managed by the persistence unit 
} 
if (managedType != null) { 
    config.addAnnotatedClass(aClass); 
} 

各永続ユニットに対して異なるConfigurationのインスタンスを使用してください。それ以外の場合は、注釈付きのクラスだけが蓄積され、DDLも蓄積されます。

私はそれを試してみました。これは2つの異なるパーシスタンスユニットに対して2つの異なるDDLを表示しました。

+0

クラスには注釈が付けられます。アノテーション用のjarファイルをスキャンすることも可能ですが、persistence.xmlファイルを解析して、どの永続性ユニットに所属する\ @Entityが属しているかを解析する必要があります。 – Matteo

+0

@Matteoはあなたの 'persistence.xml'を投稿してください。 – yair

+0

単一のpersistence.xmlはありませんが、それらの多く(したがって私のツールの必要性)を私は1つの例を追加します。 – Matteo