1

我有一个使用 Hibernate 3.6.3 作为 ORM 的应用程序。我创建了 hibernate.cfg.xml 配置文件并将其添加到构建路径中。这在我的开发环境中运行良好。现在我需要使用动态数据库连接凭据为生产服务器创建一个 jar。我花了几个小时搜索如何做到这一点,但所有的例子都使用:

  • Spring:不在“受祝福的技术”之列,

  • 单独的 Maven 配置文件:我现在需要生产凭证(这不会发生)。

我可以分离休眠数据库配置还是需要将其作为参数传递并以编程方式配置休眠?

4

1 回答 1

1

您已经提出了两种解决方案:

  • 构建时配置(使用 Maven),但您在构建时没有凭据

  • 运行时配置解析,可以这样做:

    1. 使用 Spring(你不能使用)

    2. 使用您自己的编程机制:

      Configuration configuration = new Configuration();
      configuration.configure();
      
      configuration.setProperty("hibernate.connection.url", dbUrl);
      configuration.setProperty("hibernate.connection.username", dbUser);
      configuration.setProperty("hibernate.connection.password", dbPassword); 
      
      return configuration.buildSessionFactory();
      
于 2015-03-31T10:28:15.310 回答