Setting properties programmatically in Hibernate

前端 未结 5 812
别那么骄傲
别那么骄傲 2020-11-30 11:27

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

相关标签:
5条回答
  • 2020-11-30 11:31

    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" ); 
    
    0 讨论(0)
  • 2020-11-30 11:34
    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().

    0 讨论(0)
  • 2020-11-30 11:35

    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.

    0 讨论(0)
  • 2020-11-30 11:42

    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;
        }
    }
    
    0 讨论(0)
  • 2020-11-30 11:46

    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.

    0 讨论(0)
提交回复
热议问题