Mat*_*teo 5 java hibernate persistence-unit
上一个问题的后续问题:使用Hibernate 4生成SQL DB创建脚本
目标是让命令行工具能够生成具有给定持久性单元的SQL模式的文件(类似于Hibernate Tools中存在的hibernatetool-hbm2ddl Ant任务).
根据我之前的问题的答案,这可以实现org.hibernate.tool.hbm2ddl.SchemaExport
.
而不是将所有实体添加到Configuration
(如上一个答案中所建议的)我想指定一个PersistenceUnit
.
是否可以在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" );
...
Run Code Online (Sandbox Code Playgroud)
按照评论中的要求编辑样本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>
Run Code Online (Sandbox Code Playgroud)
好吧,如果您的类是通过xml映射映射hbm
的 - 您可以Configuration
使用config.addJar(myJarFile)
和将包含xmls的documnets或jar文件直接添加到实例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 );
}
Run Code Online (Sandbox Code Playgroud)
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 );
}
Run Code Online (Sandbox Code Playgroud)
确保Configuration
为每个持久性单元使用不同的实例.否则,带注释的类只会累积,DDL也会累积.
我尝试了它并且效果很好 - 为两个不同的持久性单元打印了两个不同的DDL.
归档时间: |
|
查看次数: |
3194 次 |
最近记录: |