gpt4 book ai didi

java - Hibernate SchemaExport 和持久化单元

转载 作者:搜寻专家 更新时间:2023-10-31 19:56:33 25 4
gpt4 key购买 nike

我之前问题的后续问题: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" );
...

编辑作为示例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>

最佳答案

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

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

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

更新 2

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

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。

关于java - Hibernate SchemaExport 和持久化单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14161090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com