休眠在运行时更改cfg属性 [英] hibernate changing cfg properties at runtime

查看:54
本文介绍了休眠在运行时更改cfg属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在运行时更改cfg属性.例如:

I'm trying to change cfg properties at runtime. For example:

cfg.setProperty("hibernate.connection.url")

问题在于,只有在cfg文件本身中未定义此属性时,它才有效,意思是,它不会被覆盖.

The problem is that it works only when this property is not defined in the cfg file itself, meaning, it doesn't override.

可以通过某种方式完成吗?

Can it be done in some way?

推荐答案

运行时

Configuration cfg = new Configuration().configure();

.configure()从XML读取数据,并且其优先级高于程序配置.

the .configure() reads the data from the XML, and it has a higher priority over the programmatic configuration.

但是,如果删除 .configure ,将从您将通过的设置中读取"所有配置.例如:

However, if you remove the .configure, all the configuration will be "read" from the settings that you will pass. For example:

       Configuration configuration = new Configuration()
       .setProperty( "hibernate.connection.driver_class", "org.postgresql.Driver" )
       .setProperty( "hibernate.dialect","org.hibernate.dialect.PostgreSQLDialect")
       [...snip...]
       .addAnnotatedClass( com.myPackage.MyClass.class )
       [...] ;

将在运行时设置所有属性.

will set all the properties at runtime.

这篇关于休眠在运行时更改cfg属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆