我如何手动打开休眠会话? [英] How do I manually open a hibernate session?

查看:98
本文介绍了我如何手动打开休眠会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个涉及spring和hibernate的相当大的项目。现在,我支持某些对象从休眠状态进入内存,并且遇到了一些障碍。我有以下设置。



类A包含许多基元和一个类B. B包含原始类和一个C类,它以前是延迟加载的



现在我有这个

服务电话1:

1.)创建类A的对象

2.)获取类B的对象

3.)在A中设置B
4.)添加A到内存



服务调用2:

1.)从内存中获取A
2.)从A获得B >
3.)从C获得C

4.)在C上操作

由于C是懒加载的,它依赖于休眠会话存在以休眠方式从B延迟加载自己,至少我相信这是如此。然而,现在我需要懒加载而不修改DAO来返回一个ID,并且存在没有当前的 hibernate会话来劫持OpenSessionInView。考虑到这些限制,解决此问题的最佳方法是什么?我发现的唯一解决方案依赖于不适合的代码更改或现有会话,所以我认为我可以手动打开一个hibernate会话。我会如何去做这件事?另外,还有更好的解决方案吗?

能够在其上调用openSession()。


I have a rather large project involving spring and hibernate. Right now, I'm backing certain objects out of hibernate and into memory, and I've hit a sort of snag. I have the following setup.

Class A contains a number of primitives and a class B. B contains primitives and a class C, which was previously lazy-loaded.

Now I have this

Service call 1:
1.) create object of class A
2.) get object of class B
3.) set B in A
4.) add A to memory

Service call 2:
1.) get A from memory
2.) get B from A
3.) get C from B
4.) operate on C

Because C is lazy loaded, it relied on a hibernate session existing to lazily load itself from B with hibernate, at least I believe this to be so. Now, however, I need to lazy load without modifying the DAO to return an ID, and there exists no current hibernate session to hijack into with OpenSessionInView. What's the best way to go about solving this problem, given the limitations? The only solutions I've found rely on unsuitable code change or an existing session, so I think that I could perhaps manually open a hibernate session. How would I go about doing this? Alternatively, is there a better solution to this problem?

解决方案

If you can get a reference to your configured SessionFactory, you should be able to just call openSession() on it.

这篇关于我如何手动打开休眠会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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