【问题标题】:How to externalize Hibernate 3.6 configuration如何外部化 Hibernate 3.6 配置
【发布时间】:2015-03-31 09:21:24
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: java hibernate orm configuration hibernate.cfg.xml


    【解决方案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();
        

    【讨论】:

    • 我希望有办法以某种方式将配置提取到 MyConf.properties,但“configuration.setProperty”可以。
    猜你喜欢
    • 2019-07-15
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 2021-11-24
    • 2014-08-07
    • 2013-09-13
    相关资源
    最近更新 更多