【问题标题】:Hibernate SchemaExport and Persistence unitHibernate SchemaExport 和持久性单元
【发布时间】:2012-12-19 02:59:27
【问题描述】:

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

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

根据我上一个问题的答案,这可以通过org.hibernate.tool.hbm2ddl.SchemaExport 来实现。

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

是否可以添加一个持久化单元到 Hibernate 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" );
...

编辑根据 cmets 中的要求,一个示例 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>

【问题讨论】:

  • 所以你想保存config.addAnnotatedClass(MyMappedPojo1.class); 行?
  • @yair 是的,我想避免手动指定所有类(并避免对它们进行硬编码)。我知道我可以解析 persistence.xml 文件,但我怀疑有更简单的方法。
  • 我认为您还缺少将方言传递到配置中 - 创建 SchemaExport 时它将失败。

标签: java hibernate persistence-unit


【解决方案1】:

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

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

可以实现自己的扫描逻辑并使用config.addAnnotatedClass(myAnnotatedClass) 添加所有带注释的类(或者根据您知道的包含 ORM 类的特定包执行此操作,从而可能节省一些时间)。

更新 2

哦,更好的是,您可以通过 getManagedTypes() 迭代持久性单元的 ManagedTypes:

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

更新

您可以确定每个EntityPersistenceUnit - 无需解析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。

【讨论】:

  • 类被注释了。扫描 jar 文件中的注释可能是一种选择,但我仍然必须解析解析 persistence.xml 文件以了解给定 \@Entity 属于哪个持久性单元
  • @Matteo 你能把你的persistence.xml发一下吗?
  • 没有单一的 persistence.xml,但其中有很多(因此需要我的工具)我将添加一个示例。
  • 好的,但是要获得类,我必须列出类路径中存在的所有类,还是我弄错了?我希望有一种方法可以简单地列出由给定持久性单元管理的类,但您的解决方案可以:-)
  • @Matteo 看来你是对的, 更好的解决方案。再次查看更新 2
猜你喜欢
  • 1970-01-01
  • 2020-05-29
  • 2011-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-17
  • 2013-03-13
相关资源
最近更新 更多