我上一个问题的后续问题:Generate an SQL DB creation script with Hibernate 4
目标是拥有一个命令行工具,能够使用给定持久性单元的SQL模式生成一个文件(类似于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" );
...按照注释中的要求编辑,这是一个示例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>发布于 2013-01-07 09:12:22
如果您的类是通过xml映射(hbms)映射的--您可以使用config.addJar(myJarFile)和config.add(myXmlFile)将包含xml的文档网或jar文件直接添加到Configuration实例中。
但是,如果您希望对带注释的类进行扫描--我知道通过Hibernate没有这样简单的选项(addPackage添加元数据而不是类)。
您可以实现自己的扫描逻辑,并使用config.addAnnotatedClass(myAnnotatedClass)添加所有带注释的类(或者根据您知道的包含ORM类的特定包这样做,这样可能会节省一些时间)。
更新2
哦,更好的是,您只需通过getManagedTypes()迭代持久性单元的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 );
}更新
您可以通过检查相关的xml来确定每个xml的PersistenceUnit -而不需要解析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。
https://stackoverflow.com/questions/14161090
复制相似问题