在 Hibernate 中以编程方式设置属性 [英] Setting properties programmatically in Hibernate
本文介绍了在 Hibernate 中以编程方式设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何确保从 hibernate.cfg.xml 加载所有属性,然后以编程方式添加其他属性?我看到了以下代码片段,但它看起来像是一个全新的配置,而不是对现有配置的补充.
How can I ensure that all properties are loaded from hibernate.cfg.xml, then add additional properties programmatically? I saw the following code snippet but it looks like a completely new configuration, not an addition to an existing one.
Configuration c = new Configuration();
c.configure();
c.setProperty("hibernate.connection.username", "abc" );
c.setProperty("hibernate.connection.password", "defgh629154" );
推荐答案
您展示的代码片段正是您所需要的.只需使用您现有的配置,而无需创建新配置.
The code snippet you showed is what you need. Just use your existing configuration instead of creating a new one.
如果不是您实例化配置(但是,例如,spring),您需要扩展创建它的类.
If it is not you who instantiates the configuration (but, for example, spring), you'd need to extend the class that creates it.
这篇关于在 Hibernate 中以编程方式设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文