`
sophia_230
  • 浏览: 118624 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

hibernate 延迟加载的错误 failed to lazily initialize

阅读更多

hibernate 延迟加载的错误 failed to lazily initialize a collection of role

 

这个问题一般出现在一对多的情况下,解决的方法有两种
1、设置lazy=false
如果是用annotation,则配置如下
@OneToMany(
   targetEntity = CourseAuthorizationItem.class,
   cascade = {CascadeType.PERSIST, CascadeType.MERGE},
   mappedBy = "course", fetch=FetchType.EAGER
  )
 
将fetch类型设置成直接获取

2、就是使用filter,过滤所有的链接
如果在使用filter的时候,要配置事务处理,否则会导致session处于只读状态而不能做修改、删除的动作
<web-app>

<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate.support.OpenSessionInViewFilter
</filter-class>
</filter>

<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

</web-app> 
 Spring为我们解决最让人头痛的难题之一,Hibernate的Session的关闭与开启问题。
Hibernate 允许对关联对象、属性进行延迟加载,但是必须保证延迟加载的操作限于同一个 Hibernate Session 范围之内进行。如果 Service 层返回一个启用了延迟加载功能的领域对象给 Web 层,当 Web 层访问到那些需要延迟加载的数据时,由于加载领域对象的 Hibernate Session 已经关闭,这些导致延迟加载数据的访问异常。而Spring为我们提供的OpenSessionInViewFilter过滤器为我们很好的解决了这个问题。OpenSessionInViewFilter的主要功能是使每个请求过程绑定一个 Hibernate Session,即使最初的事务已经完成了,也可以在 Web 层进行延迟加载的操作。OpenSessionInViewFilter 过滤器将 Hibernate Session 绑定到请求线程中,它将自动被 Spring 的事务管理器探测到。所以 OpenSessionInViewFilter 适用于 Service 层使用HibernateTransactionManager 或 JtaTransactionManager 进行事务管理的环境,也可以用于非事务只读的数据操作中。
request-->open session-->打开连接、开始事务-->持久操作-->渲染(关闭连接、session)-->response
其中一些过程省略了,不是很关心。
分享到:
评论
2 楼 youjianbo_han_87 2009-09-10  
这样做纯粹只是让程式不会报错而已,根本就没有说到出现这种问题的根本原因
1 楼 fxzjdtk 2009-02-03  
LZ,你的第2个方法,我试了,可以。
如果设置fetch=FetchType.EAGER,这样每次请求都会把相关的查询出来(不需要的数据也查出来,效率不高)
也不用再在每个DAOImpl类里的方法中进行对需要initialize的集合进行initialize了(很麻烦)。
不知道偶说的对不对?

相关推荐

    Qazy:延迟加载 - 没有 SEO 负面影响

    延迟加载 - 没有 SEO 负面影响 Qazy 是一个图像延迟加载器,并且与库无关(没有 jQuery)。 Qazy 消除了。 这就是它与其他惰性加载器的不同之处。 尽快在网页中加载脚本,以便它可以开始跟踪图像并延迟加载它们。 ...

    react-lazily-img:React Wrapper组件使用IntersectionObserver API延迟加载图像

    懒惰地ReactIMG React Lazily IMG是一个React Wrapper组件,用于延迟加载图像。 目标是使用方便且已知的标准HTML标记,并只是延迟加载它们。特征图片标签和IMG srcset支持Webp检测占位符HTML && CSS图像支持下载图像...

    react-lazily-render:延迟安装昂贵的组件,直到将占位符组件滚动到视图中为止

    延迟安装昂贵的组件,直到占位符组件滚动到视图中为止。 安装 npm install --save react-lazily-render 用法 () import React from 'react' ; import LazilyRender from 'react-lazily-render' ; ...lots of ...

    Git-2.21.0-64-bit.zip

    committed, the command line prompt script failed to notice the current status, which has been improved. * Many GIT_TEST_* environment variables control various aspects of how our tests are run, ...

    JavaScript Concurrency pdf 无水印 0分

    Compute values lazily and avoid unnecessary memory allocations using generators Write concurrent code that doesn't feel like concurrent code by abstracting away boilerplate chores Leverage true ...

    实现任意多个视图内容的滚动视图

    源码实现了怎样支持延迟加载(lazily load),可以任意多个视图内容的ScrollView。所谓延迟加载,即只加载需要显示的页面内容,即按需加载,从而当需要加载许多视图内容时,不会消耗过多内存。也支持循环滚动视图。...

    Pandas Cookbook 2017 pdf 2分

    Slicing rows lazily Getting ready How to do it... How it works... There's more... Slicing lexicographically Getting ready How to do it... How it works... There's more... 5. Boolean Indexing ...

    springframework.5.0.12.RELEASE

    HibernateTransactionManager (unintentionally) bound to Hibernate 5.2 SharedSessionContractImplementor [SPR-17557] #22089 Spring JavaMailSenderImpl does not show proper message when recipient list is ...

    Lazy ScrollView(iPhone源代码)

     支持延迟加载(lazily load) 任意多个视图内容的ScrollView。所谓延迟加载,即只加载需要显示的页面内容,即按需加载,从而当需要加载许多视图内容时,不会消耗过多内存。也支持循环滚动视图。 Code4App编译测试...

    stream.js Javascript

    stream.js 是一个很小、完全独立的Javascript类库,它为你提供了一个新的...他的这种魔力来自于具有延后(lazily)执行的能力。这简单的术语完全能表明它们可以加载无穷多的元素。(http://www.aqee.net/docs/stream/)

    Redis的Scala客户端Scredis.zip

    // Subscribes to a Pub/Sub channel using the internal, lazily initialized SubscriberClient redis.subscriber.subscribe("My Channel") {  case message @ PubSubMessage.Message(channel, ...

    Android代码-Kodein-DI

    Lazily instantiate your dependencies when needed Stop caring about dependency initialization order Easily bind classes or interfaces to their instance or provider Easily debug your dependency bindings...

    Android代码-clojure-jsr223

    lazily. Copyright (c) 2009 Armando Blancas. All rights reserved. The use and distribution terms for this software are covered by the Eclipse Public License 1.0 ...

    get-all-files::high_voltage:具有懒惰同步和异步迭代器支持的快速递归目录搜寻器

    具有延迟同步和异步迭代器支持的出色的快速递归目录搜寻器。 安装 支持Node.js版本10及更高版本。 $ npm i get-all-files 用法 import getAllFiles from 'get-all-files' // Get array of filenames ...

    cinnamon:一个简单的部署工具

    名称 Cinnamon-简约的部署工具 概要 use strict; use warnings; # Exports some commands use Cinnamon::DSL; my $application = 'My::App';...# Lazily evaluated if passed as a code set lazy_value =&gt; sub { #.

    Python Cookbook英文版

    8.11 Establishing Database Connections Lazily 8.12 Accessing a JDBC Database from a Jython Servlet 8.13 Module: jet2sql-Creating a SQL DDL from an Access Database 9. User Interfaces 9.1 ...

    Android组件ViewStub基本使用方法详解

    “A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime. When a ViewStub is made visible, or when inflate() is invoked, the layout resource is ...

    svc.rar_Will

    Will be lazily created when running layout tests.namespace content.

    Android代码-ixjava

    The aim is to provide, lazily evaluated, pull-based datastream support with the same naming as in RxJava mainly for the pre-Java-8 world. The Stream API in Java 8 is not exactly the same thing because...

    OSGI in Action

    9.3 Starting bundles lazily 314 Understanding activation policies 315 ■ Using activation policies 316 9.4 Summary 317 10 Managing applications 319 10.1 Deploying bundles 320 Introducing management ...

Global site tag (gtag.js) - Google Analytics