简体   繁体   English

如何外部化Hibernate 3.6配置

[英]How to externalize Hibernate 3.6 configuration

I have an app which uses Hibernate 3.6.3 as the ORM. 我有一个使用Hibernate 3.6.3作为ORM的应用程序。 I created the hibernate.cfg.xml configuration file and added it to the build path. 我创建了hibernate.cfg.xml配置文件并将其添加到构建路径。 This works fine in my development environment. 在我的开发环境中,这很好用。 Now I need to create a jar for the production server with a dynamic DB connection credentials. 现在,我需要使用动态数据库连接凭据为生产服务器创建一个jar。 I spent couple of hours on searching how to do it but all of the examples use: 我花了几个小时来研究如何做,但是所有示例都使用:

  • Spring: which is not on the list of "blessed technologies", 春季:不在“最受祝福的技术”列表中,

  • Separate maven profiles: for which I need to now the production credentials (this won't happen). 单独的Maven配置文件:现在我需要为其提供生产凭证(这不会发生)。

Can I separate hibernate DB configurations or I need to pass it as parameter and configure hibernate programmatically? 是否可以分离休眠数据库配置,或者需要将其作为参数传递并以编程方式配置休眠?

You already proposed two solutions: 您已经提出了两种解决方案:

  • build-time configuration (using Maven), but you don't have the credentials at build-time 构建时配置(使用Maven),但是您在构建时没有凭据

  • run-time configuration resolving, which can be done: 运行时配置解析,可以完成:

    1. Using Spring (which you can't use) 使用Spring(您不能使用)

    2. Using your own programmatic mechanism: 使用您自己的编程机制:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM