如何获得Hibernate配置属性? [英] How to get Hibernate configuration properties?

查看:123
本文介绍了如何获得Hibernate配置属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jpa上使用hibernate,并使用persistence.xml进行配置
是否可以从Web应用程序获取hibernate连接属性?

I`m using hibernate with jpa, and it is configured with persistence.xml Is it possible to get hibernate connection properties from web application?

谢谢。

推荐答案

可能不是没有使用反射,并且依靠Hibernate在将来不会破坏您的代码。您需要从SessionFactory获取属性,但它不是公共的,因此您必须通过反射来查找字段,然后使用field.setAccessible来访问它。例如:

Probably not without using reflection and relying on the Hibernate not to break your code in the future. You need to get the properties from the SessionFactory but it's not public so you would have to find the Field via reflection, then use field.setAccessibleto get access to it. Something like:

Field f = SessionFactoryImpl.class.getDeclaredField("properties");
f.setAccessible(true);
Properties p = (Properties)f.get(sessionFactory);

然后使用Environment中的常量来提取相关设置。如果您正在查找实际的数据库连接设置并且您的应用程序正在使用jndi,那么可以使用jndi名称来获取DataSource并检查它以获取连接信息。

Then use the constants in Environment to pull out the relevant settings. If you're looking for the actual database connection settings and your app is using jndi, then it's possible you can use the jndi name to get a DataSource and examine that to get connection information.

对于这种类型的东西,我通常只使用调试器,设置一个断点,然后在变量周围徘徊,直到找到信息的位置;然后查看它是否公开可用,如果不是,则使用反射来实现它。虽然没有保证。

For this type of thing I usually just use the debugger, set a breakpoint, and then poke around the variables until I find where the information is; then see if it's publicly available and if not, use reflection to get to it. There are no guarantees though.

这篇关于如何获得Hibernate配置属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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