Setting properties programmatically in Hibernate

前端 未结 5 935
别那么骄傲
别那么骄傲 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: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;
        }
    }
    

提交回复
热议问题