Monday, 7 September 2009

How To Override Hibernate Configuration Settings?

 The following code segment will override the some properties. [hibernate.connection.driver_class, hibernate.connection.url ,hibernate.connection.url, hibernate.connection.password]


    public static void createSessionFactory(String driverclass,String url, String username, String password, String configFile) throws HibernateException {
        if (sessionFactory != null) {
            sessionFactory.close();
        }
        Configuration cfg = new AnnotationConfiguration().addResource(configFile);
        cfg.configure(configFile);
        cfg.setProperty("hibernate.connection.driver_class", driverclass);
        cfg.setProperty("hibernate.connection.url", url);
        cfg.setProperty("hibernate.connection.url", username);
        cfg.setProperty("hibernate.connection.password", password); 
        sessionFactory = cfg.buildSessionFactory();
    }

       Hibernate configuration files properties used in following implementation.

    public static void createSessionFactory(String driverclass,String url, String username, String password, String configFile) throws HibernateException {
        if (sessionFactory != null) {
            sessionFactory.close();
        }
        Configuration cfg = new AnnotationConfiguration().addResource(configFile);     
        cfg.setProperty("hibernate.connection.driver_class", driverclass);
        cfg.setProperty("hibernate.connection.url", url);
        cfg.setProperty("hibernate.connection.url", username);
        cfg.setProperty("hibernate.connection.password", password); 
        cfg.configure(configFile);
        sessionFactory = cfg.buildSessionFactory();
    }

2 comments:

roger said...

annotationconfiguration is deprecated?

Thillakan said...

Hi Roger,

AnnotationConfiguration is deprecated in Hibernate 3.6, and all its functionality has been moved to Configuration. So, you could use Configuration.
http://docs.jboss.org/hibernate/core/4.0/javadocs/org/hibernate/cfg/Configuration.html
http://docs.jboss.org/hibernate/core/4.0/javadocs/org/hibernate/cfg/Configuration.html

By the way this is posted in 2009 :).