我有 hibernate.cfg.xml 文件。
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url"></property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
......................
这是文件中最有趣的部分。 现在我必须设置缺失值:url、用户名、密码。 我正在尝试这样做:
public static void SetSessionFactory() {
try {
AnnotationConfiguration conf = new AnnotationConfiguration().configure();
// <!-- Database connection settings -->
conf.setProperty("connection.url", URL);
conf.setProperty("connection.username", USERNAME);
conf.setProperty("connection.password", PASSWORD);
SESSION_FACTORY = conf.buildSessionFactory();
} catch (Throwable ex) {
// Log exception!
throw new ExceptionInInitializerError(ex);
}
}
但它只是从 hibernate.cfg.xm 加载我的配置并且不更改任何属性...
url、username、passoword - 是命令行参数,所以我必须在运行时设置它们。
最佳答案
尝试在此处调用 conf.configure();
。
并且属性可能需要有像“hibernate.connection.username”这样的 hibernate 前缀
希望对您有所帮助。
关于java - 运行时的 Hibernate 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1341871/