【问题标题】:Java: Hibernate don't import filesJava:休眠不导入文件
【发布时间】:2016-09-23 07:46:18
【问题描述】:

我的 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 文件。我希望任何人都可以帮助我。

问候

【问题讨论】:

  • 是否假设使用不同的.sql 文件?
  • @ScaryWombat 我使用了路径p.put("hibernate.hbm2ddl.import_files", "/resources/facttable.sql");,但它也不起作用

标签: java hibernate config


【解决方案1】:

根据链接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");

【讨论】:

  • 但我没有/WEB-INF/ 目录
  • 哦,好的,不是网络应用程序,然后尝试完全路径文件
  • 我试过/src/test/resources/facttable.sql,但也没用
  • 你有一个完整的路径文件,比如/src/test/resources/facttable.sql 还是在你的项目目录下面?
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 2013-12-11
      相关资源
      最近更新 更多