首页 \ 问答 \ 保持Hibernate会话通过注释失效?(Keeping Hibernate Session from Expiring via Annotations? — failed to lazily initialize a collection of role)

保持Hibernate会话通过注释失效?(Keeping Hibernate Session from Expiring via Annotations? — failed to lazily initialize a collection of role)

当试图访问一个包/集合的返回对象的成员时,我有一个hibernate DAO抛出一个“无法懒惰地初始化一个角色集合”异常。

我理解抛出异常的问题的范围。 Hibernate返回我的对象​​,并且对于任何集合,返回代理对象。 在我的调用者中,当我去访问这些代理对象时, 因为hibernate会话已经过期 ,所以抛出了这个异常。

我想知道的是,如何避免会话使用注释过期? 可能吗?

例如,如果我的调用方法是:

@RequestMapping("refresh.url")
public ModelAndView refresh(HttpServletRequest request, HttpServletResponse response, int id) throws Exception {
    TestObject myObj = testObjDao.get(id);
    // throws the exception
    myObj.getCollection();

我将如何使用注释来防止此异常? 我知道一个解决方案是通过一个以伪代码形式显示的回调来扩展hibernate会话:

@RequestMapping("refresh.url")
public ModelAndView refresh(HttpServletRequest request, HttpServletResponse response, int id) throws Exception {
    session = get hibernate session...
    session.doInSession(new SessionCallback() {
        TestObject myObj = testObjDao.get(id);
        // no longer throws the exception
        myObj.getCollection();
    });

但是这在我需要访问集合的所有函数中似乎都是重复的。 是不是有一种方法可以在那里简单地拍@Transactional注释并完成它? 如下所示:

@RequestMapping("refresh.url")
@Transactional  // this doesn't extend the session to this method?
public ModelAndView refresh(HttpServletRequest request, HttpServletResponse response, int id) throws Exception {
    TestObject myObj = testObjDao.get(id);
    // throws the exception
    myObj.getCollection();

感谢您的帮助,向我解释这一点。


I have a hibernate DAO that is throwing a "failed to lazily initialize a collection of role" Exception when trying to access a member of the returned object that is a bag/Collection.

I understand the scope of the problem throwing the exception. Hibernate returns my object, and for any Collections, returns proxy objects. In my caller, when I go to access those proxy objects, because the hibernate session has expired, this exception is thrown.

What I want to know is, how can I keep the session from expiring using annotations? Is it possible?

For instance if my calling method is:

@RequestMapping("refresh.url")
public ModelAndView refresh(HttpServletRequest request, HttpServletResponse response, int id) throws Exception {
    TestObject myObj = testObjDao.get(id);
    // throws the exception
    myObj.getCollection();

How would I prevent this exception using annotations? I know one solution would be to extend the hibernate session via a callback which in pseudocode might look something like:

@RequestMapping("refresh.url")
public ModelAndView refresh(HttpServletRequest request, HttpServletResponse response, int id) throws Exception {
    session = get hibernate session...
    session.doInSession(new SessionCallback() {
        TestObject myObj = testObjDao.get(id);
        // no longer throws the exception
        myObj.getCollection();
    });

but this seems rather repetitive to have in all of my functions that need to access collections. isn't there a way to simply slap an @Transactional annotation on there and be done with it? as in:

@RequestMapping("refresh.url")
@Transactional  // this doesn't extend the session to this method?
public ModelAndView refresh(HttpServletRequest request, HttpServletResponse response, int id) throws Exception {
    TestObject myObj = testObjDao.get(id);
    // throws the exception
    myObj.getCollection();

thanks for your help in explaining this to me.


原文:https://stackoverflow.com/questions/6316124
更新时间:2024-02-12 20:02

最满意答案

DTO的规范定义是没有任何行为的对象的数据形状。

ViewModels是视图的模型。 ViewModel通常是来自一个或多个对象(或DTO)的完整或部分数据,以及特定于视图行为的任何附加成员(可由视图执行的方法,指示切换视图元素等的属性等)。 您可以将viewmodel视为视图加行为的所有数据。 ViewModel可能会也可能不会将一对一映射到业务对象或DTO。

顺便说一句,如果某个viewmodel需要一个持久对象的数据子集,NHibernate的预测就会派上用场。


The canonical definition of a DTO is the data shape of an object without any behavior.

ViewModels are the model of the view. ViewModels typically are full or partial data from one or more objects (or DTOs) plus any additional members specific to the view's behavior (methods that can be executed by the view, properties to indicate how toggle view elements etc...). You can look at the viewmodel as all the data for a view plus behaviors. ViewModels may or may not map one to one to business objects or DTOs.

By the way, NHibernate projections come in handy if a certain viewmodel needs a subset of the data from a persisted object.

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。