Java: Hibernate 不导入文件

Java: Hibernate don't import files

我的 Hibernate Config 不导入任何 .sql 文件。 文件位于 src/test/resources,源代码位于 src/test/java

配置部分:

Configuration configuration = new Configuration();
configuration.addAnnotatedClass(SomeModel.class);
Properties p = new Properties();
p.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
p.put("hibernate.connection.driver_class", "org.h2.Driver");
p.put("hibernate.connection.url", "jdbc:h2:mem:test");
p.put("hibernate.show_sql", "true");
p.put("hibernate.hbm2ddl.auto", "create-drop");
p.put("hibernate.hbm2ddl.import_files", "/createdb.sql");
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(p).build();
this.sf = configuration.buildSessionFactory(serviceRegistry);

我不知道他为什么不导入.sql文件。我希望任何人都可以帮助我。

问候

根据 link hibernate.hbm2ddl.import_files: Path to the files

路径需要设置为

<prop key="hibernate.hbm2ddl.import_files">/WEB-NF/resources/createdb.sql</prop>

或编程为

p.put("hibernate.hbm2ddl.import_files", "/WEB-INF/resources/createdb.sql");

因为,您的文件 facttable.sql 位于 src/test/resources 文件夹中。您需要使用以下代码才能访问它。

p.put("hibernate.hbm2ddl.import_files", "src\test\resources\facttable.sql");

导入文件是从资源根目录下查找的,即src/main/resources.
因此,如果您将 SQL 文件放在文件夹 sql 下,例如src/main/resources/sql/createdb.sql,那么这应该有效:

<prop key="hibernate.hbm2ddl.import_files">sql/createdb.sql</prop>