【问题标题】:How to execute differnet import.sql in Hibernate/JPA for each persistence unit?如何在 Hibernate/JPA 中为每个持久性单元执行不同的 import.sql?
【发布时间】:2010-10-19 00:37:08
【问题描述】:

我在我的 JPA/Hibernate 配置中配置了两个持久性单元。现在我需要为每个持久性单元执行不同的 import.sql。如何指定应为每个持久性单元执行哪个 import.sql?根据 Hibernate 文档,我应该将 import.sql 放在类路径中。如果我这样做,import.sql 会在每个持久性单元上执行。我需要以某种方式为每个持久性单元指定不同的 import.sql。

【问题讨论】:

    标签: hibernate jpa import persistence-unit


    【解决方案1】:

    FWIW,这在 Hibernate 3.6.0.Beta1 中是可能的(参见 HHH-5337),您现在可以使用 hibernate.hbm2ddl.import_files 属性声明要导入的文件:

    hibernate.hbm2ddl.import_files /mydbload.sql,/mydbload2.sql
    

    因此您可以为每个持久性单元使用不同的值。

    【讨论】:

      【解决方案2】:

      您可以在应用程序启动时使用 org.hibernate.tool.hbm2ddl.SchemaExport 类手动执行一些操作。

      SchemaExport schemaExport1 = new SchemaExport(cfg1); // there are various c-tors available
      schemaExport1.setInputFile("/import-1.sql");
      schemaExport1.create(false, true);
      
      SchemaExport schemaExport2 = new SchemaExport(cfg2);
      schemaExport2.setInputFile("/import-2.sql");
      schemaExport2.create(false, true);
      

      【讨论】:

      • 方法名为SchemaExport.setImportFile(String)
      【解决方案3】:

      在我所有的项目中,我只使用一个 import.sql,在它旁边我创建不同的其他 *.sql(例如:H2_import.sql,sqlServer_import.sql),并根据要使用的持久性单元复制*.sql 并将其传递到 import.sql

      【讨论】:

        猜你喜欢
        • 2011-04-21
        • 2013-02-15
        • 1970-01-01
        • 2017-08-18
        • 1970-01-01
        • 2021-02-17
        • 2014-06-25
        • 2012-12-19
        • 1970-01-01
        相关资源
        最近更新 更多