繁体   English   中英

如何更改cfg.xml文件的属性?

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

我正在尝试以编程方式更改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文件中的旧属性。

我猜您需要在手动设置属性时指定属性的全名:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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