2013-01-04 132 views
5

一个后续问题我刚才的问题:Generate an SQL DB creation script with Hibernate 4休眠的SchemaExport和持久性单元

的目标是有能够产生与给定的持久性单元(同样的SQL架构到hibernatetool-文件中的命令行工具就是hbm2ddl蚂蚁出现在休眠的工具任务)。

根据我对上一个问题的回答,可以用org.hibernate.tool.hbm2ddl.SchemaExport来实现。

而不是将所有实体添加到Configuration(如前面的答案中所建议的),我想指定一个PersistenceUnit

是否有可能加入休眠单元休眠Configuration

喜欢的东西

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"); 
... 

编辑作为评论的样品persistence.xml要求。每个类都被注解@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

好了,如果你的类是通过XML映射(hbm S)映射 - 您可以添加包括个XML直接使用config.addJar(myJarFile)config.add(myXmlFile)Configuration实例documnets或jar文件。

但是,如果你希望你的注解类进行扫描 - 我知道通过Hibernate没有这种直接的选项(addPackage添加元数据和类)。

可能实现自己的扫描逻辑,并与config.addAnnotatedClass(myAnnotatedClass)添加注释的所有类(或者做每您知道包含您ORM类,因此可能节省一些时间特定的包)。

更新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

您可以确定每个PersistenceUnitEntity - 不必解析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也会累积。

我试过了,它效果很好 - 为两个不同的持久性单元打印了两个不同的DDL。

+0

这些类是注释。扫描注释的jar文件可能是一个选项,但我仍然必须分析解析persistence.xml文件,以知道给定的@ @Entity属于哪个持久性单元 – Matteo

+0

@Matteo可以请您发布您的'persistence.xml'吗? – yair

+0

没有单个persistence.xml,但其中很多(因此需要我的工具)我将添加一个示例。 – Matteo