简体   繁体   中英

how can I change the properties of the cfg.xml file?

I'm trying to change the properties of the hibernate.cfg.xml file programmatically here's what I did :

public class NewHibernateUtil {

 private final SessionFactory sessionFactory;
 String host;
 String database;
 String username;
 String password;
 NewHibernateUtil(String host,String database,String username,String password){
     this.host=host;
     this.database=database;
     this.username=username;
     this.password=password;
     AnnotationConfiguration ac=new AnnotationConfiguration().configure();
     ac.setProperty("connection.url", "jdbc:mysql://"+host+"/"+database+"");  
     ac.setProperty("connection.username", username);  
     ac.setProperty("connection.password", password);             
     sessionFactory = ac.buildSessionFactory();         
 }    

public SessionFactory getSessionFactory() {
    return sessionFactory;
}    
}

but This doesn't work,it always uses the old properties in the hibernate.cfg.xml file.

I guess you need to specify full names of the properties when setting them manually:

ac.setProperty("hibernate.connection.url", "jdbc:mysql://"+host+"/"+database+"");        
ac.setProperty("hibernate.connection.username", username);
ac.setProperty("hibernate.connection.password", password);       

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM