Search code examples
hibernatejpaimportpersistence-unit

How to execute differnet import.sql in Hibernate/JPA for each persistence unit?


I have configured two persistence units in my JPA/Hibernate configuration. Now i need to execute different import.sql for each persistence unit. How can I specify which import.sql should be executed for each persistence unit? According Hibernate to documentation, I should place import.sql in classpath. If I do that, import.sql is executed on each persistence unit. I need somehow to specify different import.sql for each persistence unit.


Solution

  • You could probably do something manual using the org.hibernate.tool.hbm2ddl.SchemaExport class when your application starts up.

    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);