本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7593594
建立时间:2006年5月29日




[Hibernate]如何操作Hibernate配置中定义的缓存
软件技术

lhwork 发表于 2007/1/19 8:52:12

Hibernate通过配置项 hibernate.cache.provider_class 来指定所采用的Cache API,但是在Hibernate所提供对缓存操作方法都是一些高层的API,也就是说Hibernate提供对缓存的操作方法都是基于对象的操作,例如 session.evict(Object obj);SessionFactory.evictXxxx 但有时候这些方法并不能完成我们想要的功能时,因此我们需要直接操控缓存来达到我们想要的目的。 挨个打开Hibernate的源代码发现了有一个类Settings,这个类有一个getCacheProvider方法,而通过SessionFactoryImpl类可以获取Settings的实例。从字面上来可以猜出SessionFactoryImpl就是对接口SessionFactory的实现,而getCacheProvider就是用来获取缓存管理器的实例,经过一番试验证实了以上猜测。 接下来可以在Hibernate的初始化时做点手脚来获取CacheProvider实例: Configuration cfg = new Configuration().configure(cfg_path);//将buildSessionFactory返回的对象强制转为SessionFactoryImpl类型SessionFactoryImpl sessionFactory = (SessionFactoryImpl)cfg.buildSessionFactory();this.cacheProvider = sessionFactory.getSettings().getCacheProvider(); 这样我们就拿到了CacheProvider实例,接下来再看看CacheProvider接口,该接口是通过buildCache(String name, Properties props)方法来创建缓存的也就是说我们还需要一个参数就是props,这个参数就是Hibernate的配置信息,再次阅读SessionFactoryImpl源码便可知道通过Configuration类来获取配置信息 所以最终完整的代码是: Configuration cfg = new Configuration().configure(cfg_path);SessionFactoryImpl ssnFactory = (SessionFactoryImpl)cfg.buildSessionFactory();CacheProvider cacheProvider = ssnFactory.getSettings().getCacheProvider();Cache cache = cacheProvider.buildCache("cache_name", cfg.getProperties());cache.get(xxxxx);cache.set(xxxx,xxxx);


阅读全文(1767) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.299 second(s), page refreshed 144756088 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号