Setting properties programmatically in Hibernate
Asked Answered
R

5

20

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" ); 
Rhona answered 20/5, 2011 at 16:7 Comment(1)
Check out the answer of #16481351.Pentad
D
15

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.

Delusive answered 20/5, 2011 at 16:51 Comment(1)
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.Rhona
I
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" ); 
Illnatured answered 20/5, 2011 at 17:1 Comment(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.Feme
@Binil, this answer did not clarify my confusion. The answer that I marked as "the answer" is the one that clarified my confusion.Rhona
A
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().

Arva answered 11/10, 2015 at 16:21 Comment(0)
K
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;
    }
}
Kcal answered 4/6, 2014 at 11:36 Comment(2)
I am confused if the property names are c3p0.max_size .. or hibernatec.3p0.max_size .. as they mention here docs.jboss.org/hibernate/orm/4.2/devguide/en-US/html/ch01.htmlNudibranch
it didn't work , i have tried it, i use Hibernate version 4.1.xAryan
L
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.

Lukewarm answered 20/5, 2011 at 16:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.