19

How can I ensure that all properties are loaded from hibernate.cfg.xml, then add additional properties programmatically? I saw the following code snippet but it looks like a completely new configuration, not an addition to an existing one.

Configuration c = new Configuration();
c.configure();

c.setProperty("hibernate.connection.username", "abc" );
c.setProperty("hibernate.connection.password", "defgh629154" ); 
KyleM
  • 4,445
  • 9
  • 46
  • 78
  • Check out the answer of http://stackoverflow.com/questions/16480851/unsupportedoperationexception-the-application-must-supply-jdbc-connections. – Siddharth May 13 '13 at 10:59

5 Answers5

14

The code snippet you showed is what you need. Just use your existing configuration instead of creating a new one.

If it is not you who instantiates the configuration (but, for example, spring), you'd need to extend the class that creates it.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Ahh. Thank you! I got confused because Hibernate is not listed in any bean anywhere. It is actually used indirectly through DWR, spring does not instantiate it. – KyleM May 20 '11 at 17:03
8
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
  private static SessionFactory sessionFactory;

  static {
    Configuration configuration = new Configuration();

    configuration.addAnnotatedClass (org.gradle.Person.class);
    configuration.setProperty("connection.driver_class","com.mysql.jdbc.Driver");
    configuration.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate");                                
    configuration.setProperty("hibernate.connection.username", "root");     
    configuration.setProperty("hibernate.connection.password", "root");
    configuration.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");
    configuration.setProperty("hibernate.hbm2ddl.auto", "update");
    configuration.setProperty("show_sql", "true");
    configuration.setProperty(" hibernate.connection.pool_size", "10");

    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
    sessionFactory = configuration.buildSessionFactory(builder.build());
  }

  public static SessionFactory getSessionFactory() {
     return sessionFactory;
  }
} 

Using .configure() makes Hibernate to look for the hibernate.cfg.xml file. So if you don't want to use the hibernate.cfg.xml file, don't use .configure().

Deepak
  • 1,670
  • 1
  • 20
  • 20
8

You code snippet should load hibernate.cfg.xml from the root of the classpath and then add or overwrite the configuration properties programmatically . So , please make sure that your so called the "existing one hibernate.cfg.xml " is on the root of the classpath.

If your "existing one hibernate.cfg.xml " is not on root of the classpath , but in some package , you can load it by specifying its package path in the configure() , likes

Configuration config = new Configuration();
config.configure("package1/package2/hibernate.cfg.xml");
config.setProperty("hibernate.connection.username", "update" );
config.setProperty("hibernate.connection.password", "defgh629154" ); 
Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • 2
    The key point is that `c.configure()` loads the configuration from `hibernate.cfg.xml` resource. The OP seem to have been unaware of that and this answer clarifies his confusion. So as such, this should have been voted as the correct answer. – Binil Thomas May 20 '11 at 20:17
  • 1
    @Binil, this answer did not clarify my confusion. The answer that I marked as "the answer" is the one that clarified my confusion. – KyleM Jun 01 '11 at 19:39
6

This is working properly than i thought

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.


            Properties c = new Properties();
            c.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
            c.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
            c.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/mydiscou_billing?zeroDateTimeBehavior=convertToNull");
            c.setProperty("hibernate.connection.username", "root");
            c.setProperty("hibernate.connection.password", "123");
            c.setProperty("hibernate.connection.autoReconnect", "true");

            c.setProperty("connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider");
            c.setProperty("c3p0.min_size", "5");
            c.setProperty("c3p0.max_size", "20");
            c.setProperty("c3p0.timeout", "1800");
            c.setProperty("c3p0.max_statements", "100");
            c.setProperty("hibernate.c3p0.testConnectionOnCheckout", "true");




            sessionFactory = new AnnotationConfiguration().setProperties(c).configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}
Akilamaxi
  • 81
  • 1
  • 3
  • I am confused if the property names are c3p0.max_size .. or hibernatec.3p0.max_size .. as they mention here https://docs.jboss.org/hibernate/orm/4.2/devguide/en-US/html/ch01.html – Abhidemon Feb 18 '16 at 11:53
  • it didn't work , i have tried it, i use Hibernate version 4.1.x – danisupr4 Nov 17 '16 at 02:01
1

Perhaps you could create your configuration like this:

Configuration cfg = new Configuration();
cfg.addResource("Hibernate.cfg.xml");

and then apply your specific property settings.

I have assumed that you do want to instantiate your Configuration yourself. If not you need to get it from whatever it is that has instantiated it e.g. Spring's LocalSessionFactoryBean if that's what you're using.

Alex Barnes
  • 7,174
  • 1
  • 30
  • 50