一个后续问题我刚才的问题:Generate an SQL DB creation script with Hibernate 4休眠的SchemaExport和持久性单元
的目标是有能够产生与给定的持久性单元(同样的SQL架构到hibernatetool-文件中的命令行工具就是hbm2ddl蚂蚁出现在休眠的工具任务)。
根据我对上一个问题的回答,可以用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>
所以你想保存'config.addAnnotatedClass(MyMappedPojo1.class);'行? – yair
@yair是的我想避免手动指定所有的类(并避免对它们进行硬编码)。我知道我可以解析persistence.xml文件,但我怀疑有一种更简单的方法。 – Matteo
我想你也错过了将方言传递给配置 - 当SchemaExport被创建时它会失败。 – kboom