Search code examples
javahibernateormconfigurationhibernate.cfg.xml

How to externalize Hibernate 3.6 configuration


I have an app which uses Hibernate 3.6.3 as the ORM. I created the hibernate.cfg.xml configuration file and added it to the build path. This works fine in my development environment. Now I need to create a jar for the production server with a dynamic DB connection credentials. 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).

Can I separate hibernate DB configurations or I need to pass it as parameter and configure hibernate programmatically?


Solution

  • You already proposed two solutions:

    • build-time configuration (using Maven), but you don't have the credentials at build-time

    • run-time configuration resolving, which can be done:

      1. Using Spring (which you can't use)

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