【问题标题】:How to export the schema for hibernate > 4.3如何导出休眠模式> 4.3
【发布时间】:2014-03-29 16:24:29
【问题描述】:

有很多关于如何以编程方式使用休眠模式导出架构的帖子(例如 [1])。

但是在 Hibernate 4.3 中,org.hibernate.ejb.Ejb3Configuration 类被删除了,我找不到任何替代品。 如何以编程方式生成休眠版本 >=4.3 的 ddl 脚本? 由于我使用 spring 来设置实体管理器,因此我也不必再使用 persitence.xml,我想保持这种方式。

[1]http://techblog.bozho.net/?p=935

【问题讨论】:

  • 您可以在哪里获得解决方案,如果可以,请发布。我处于类似的情况,我不使用persistence.xml。
  • 不抱歉,我放弃了这个——但你也许可以根据@mabi 的回答完成这项工作

标签: java spring hibernate


【解决方案1】:

为了扩展@geoand 的答案,我编写了一个独立的SchemaGenerator groovy 脚本来触发SchemaExport 类:

class SchemaGenerator {
static final void main(String[] args) {
    def outputPath = args[0]
    def classesDir = args[1]

    Configuration cfg = new Configuration()
    cfg.setProperty('hibernate.dialect', 'org.hibernate.dialect.PostgreSQLDialect')
    cfg.setProperty('hibernate.hbm2ddl.auto', 'create')

    addClasses(cfg, new File(classesDir), 'my.entity.package.prefix')

    SchemaExport export = new SchemaExport(cfg)
    export.outputFile = new File(outputPath)
    export.delimiter = ';'
    export.format = true

    export.execute(true, false, false, true)
}
// addClasses uses cfg.addAnnotatedClass(Class) where it grabs the Class instance
// with Class.forName(name) with name derived from iterating the file structure
// of the classesDir
}

诀窍是找到类文件:编译完成后,我用我的构建系统运行这个脚本,并将路径传递给我生成的类文件。

但是,从哪里获取类实例并不重要,主要是调用 cfg.addAnnotatedClass() 以使 Hibernate 了解它们。

【讨论】:

    【解决方案2】:

    看来org.hibernate.tool.hbm2ddl.SchemaExport类是Hibernate中处理DDL的类。如果您检查 Spring 日志,这是用于调用 DDL 操作的类。

    但是我不知道你如何自己使用这个类

    【讨论】:

      【解决方案3】:

      我使用org.reflections 扫描类路径以查找带有@Entity 注释的类并将它们添加到配置中。

      另外我添加了 AuditConfiguration.getFor(cfg); 生成审计表。

      之后我使用已经提到的 SchemaExport 来导出 Schema。

          //set the package to scan
          Reflections reflections = new Reflections("my.packages.dao");
      
          //find all classes annotated with @Entity        
          Set<Class<?>> typesAnnotatedWith = reflections.getTypesAnnotatedWith(Entity.class);
      
          //create a minimal configuration
          Configuration cfg = new Configuration();
          cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
          cfg.setProperty("hibernate.hbm2ddl.auto", "create");
      
          for(Class<?> c : typesAnnotatedWith){
              cfg.addAnnotatedClass(c);
          }
      
          //build all the mappings, before calling the AuditConfiguration
          cfg.buildMappings();
      
          //configure Envers
          AuditConfiguration.getFor(cfg);
      
          //execute the export
          SchemaExport export = new SchemaExport(cfg);
          export.setOutputFile(fileName);
          export.setDelimiter(";");
          export.setFormat(true);
      
          export.execute(true, false, false, true);
      

      【讨论】:

        【解决方案4】:

        this 发布我使用基于将 SessionFactory 转换为 Hibernate SessionFactoryImpl 并使用 sessionfactory 内部模式导出对象的解决方案。这不是最好的解决方案,但很有效而且很简单。

        编辑

        网址已修复。

        【讨论】:

        • @danielmelobrasil 您发布的链接假定​​有persistence.xml,但这里问题的作者没有使用persistence.xml
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-12
        • 2011-03-24
        • 1970-01-01
        • 2016-09-26
        • 2013-04-28
        • 2012-06-21
        • 1970-01-01
        相关资源
        最近更新 更多