hibernate - 如何使用 JPA 2.0 文件配置 Hibernate?

标签 hibernate jpa persistence

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/

相关文章:

java.lang.IllegalStateException : Unable to retrieve EntityManagerFactory for unitName HorizonPU with Arquillian 错误

Java持久化API : class uses non-entity class as target entity in the relationship attribute

java - 如何获取父值

java - 从 hibernate 变更集中删除实体是否与调用 EntityManger.detach() 一样简单?

java - 可以在java中创建自定义jpa注释

sql - 如何通过Hibernate获取数据库版本?

hibernate - JPA ManyToMany单向关系

session - Firebase 3.0 session 持久性

hibernate - hibernate session 中的持久数据变得陈旧,有时 hibernate 会生成重复的 ID

java - 使用 Spring 在 Hibernate 中进行水平分区