2009-04-12 31 views
2

我在JPA/Hibernate配置中配置了兩個持久性單元。現在我需要爲每個持久性單元執行不同的import.sql。我如何指定爲每個持久性單元執行哪個import.sql?根據Hibernate的文檔,我應該將import.sql放在classpath中。如果我這樣做,import.sql將在每個持久性單元上執行。我需要以某種方式爲每個持久性單元指定不同的import.sql。如何在每個持久性單元的Hibernate/JPA中執行不同的import.sql?

回答

5

當您的應用程序啓動時,您可以使用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); 
+0

該方法稱爲SchemaExport.setImportFile(String) – xmedeko 2011-01-13 13:41:05

7

FWIW,這是可能的休眠3.6.0.Beta1(見HHH-5337),你現在可以聲明一下文件(S)使用hibernate.hbm2ddl.import_files屬性導入:

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

所以,你可以爲每個持久性單元使用不同的值。

0

在我所有的項目中,我只使用一個import.sql,並在它旁邊創建不同的其他* .sql(例如:H2_import.sql,sqlServer_import.sql),並根據持久性單元使用我複製內容* .sql並將其過濾到import.sql中

相關問題