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);
精彩评论