Hibernate 文档显示得很清楚how to configure Hibernate with XML .
这是执行此操作的代码片段:
new Configuration().configure("catdb.cfg.xml")
现在,当您拥有 JPA 2.0 配置文件而不是 hibernate 配置文件时,如何配置 Hibernate?
- 如何为 META-INF/persistence.xml 执行此操作?
- 如果我的文件名为 META-INF/jpa.xml 会怎样?
这适用于 Hibernate 3.6 和 JPA 2.0。我的最终目标是能够导出文件 persistence.xml 中描述的类的模式 DDL,因此我不想构建 SessionFactory。
Configuration cfg = /* ??? */
SchemaExport schemaExport = new SchemaExport(cfg);
schemaExport.setDelimiter(";");
schemaExport.setOutputFile("ddl.sql");
boolean script = true, export = false, justDrop = false, justCreate = false;
schemaExport.execute(script, export, justDrop, justCreate);
最佳答案
假设您通常使用持久性单元名称查找配置,您可以创建一个 org.hibernate.ejb.Ejb3Configuration
并让它返回包装的 org.hibernate.cfg .配置
:
package test;
import org.hibernate.cfg.Configuration;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class SchemaExportTest {
public static void main(String[] args) {
Configuration cfg = new Ejb3Configuration().configure( "persistence-unit-name", null ).getHibernateConfiguration();
SchemaExport export = new SchemaExport(cfg);
export.execute(true, false, false, false);
}
}
关于hibernate - 如何使用 JPA 2.0 文件配置 Hibernate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6111883/