运行时休眠配置
问题描述:
我有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>
.....................
这是最有趣的部分文件。 现在我必须设置缺失值:网址,用户名,密码。 我想以这样的方式来做到:
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,用户名,passoword - 被命令行参数,所以我必须在运行时设置它们。
答
试着在这里打电话conf.configure();
。
和属性可能需要有休眠前缀,如“hibernate.connection.username”
希望它有帮助。
+0
maaaaaagic^_^ 添加了hibernate。到cfg文件和SetSessionFactory方法中的属性名称,现在所有的作品! – Oleksandr 2009-08-27 15:48:36
答
从Environment
类
答
使用常量尝试喜欢这个工作正常
AnnotationConfiguration的conf =新
AnnotationConfiguration()配置( “/ dronehibernate.cfg.xml”)。
conf.setProperty("hibernate.connection.url","jdbc:mysql://localhost/PAT_DRONE_DB1");
SessionFactory sessionFactory = conf.buildSessionFactory();
Session session = sessionFactory.openSession();
List<NetworkType> channelList = session.createQuery("from NetworkType").list();
哪个命名空间应该用于在asp.net MVC中使用“AnnotationConfiguration”? – bluwater2001 2010-03-26 18:27:42