Hibernate SchemaExport和Persistence单元

时间:2013-01-04 16:37:01

标签: java hibernate persistence-unit

上一个问题的后续问题:Generate an SQL DB creation script with Hibernate 4

目标是让命令行工具能够使用给定持久性单元的SQL模式生成文件(类似于Hibernate Tools中存在的hibernatetool-hbm2ddl Ant任务)。

根据我上一个问题的答案,这可以通过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>

1 个答案:

答案 0 :(得分:6)

好吧,如果您的类是通过xml映射(hbm s)映射的 - 您可以使用Configuration将包含xmls的documnets或jar文件直接添加到config.addJar(myJarFile)实例, config.add(myXmlFile)

但是,如果您希望扫描带注释的类 - 我知道Hibernate没有这样简单的选项(addPackage添加了元数据,不是类)

可以实现你自己的扫描逻辑并添加带config.addAnnotatedClass(myAnnotatedClass)的所有带注释的类(或者你知道包含ORM类的特定包也可以这样做,因此可以节省一些时间)

更新2

哦,甚至更好,您可以通过ManagedType迭代持久性单元的getManagedTypes()

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 );
}

<强>更新

您可以通过检查相关PersistenceUnit来确定每个Entity - EntityManagerFactory而无需解析xml

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。