我正在尝试以编程方式更改hibernate.cfg.xml文件的属性 这是我做的:
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;
}
}
但是这不起作用,它总是使用hibernate.cfg.xml文件中的旧属性。
答案 0 :(得分:1)
我猜您需要在手动设置属性时指定属性的全名:
ac.setProperty("hibernate.connection.url", "jdbc:mysql://"+host+"/"+database+"");
ac.setProperty("hibernate.connection.username", username);
ac.setProperty("hibernate.connection.password", password);