`

failed to lazily initialize a collection of role:no session or session was close

阅读更多

解决方法(延迟加载问题)
把fetch= FetchType.LAZY 改成 fetch = FetchType.EAGER


1、OpenSessionInView模式:

以下有2种方法,第1种是结合SPRING,第2种是采用了拦截器

Spring+Hibernate中,     集合映射如果使用lazy="true", 当PO传到View层时, 出现未初始化session已关闭的错误,只能在dao先初始化

parent.getChilds().size();s

Spring提供Open Session In View来解决这个问题, 有两种方式
1. Interceptor
       <!--</span><span style="COLOR: rgb(0,128,0)"> =========== OpenSession In View pattern ==============</span><span style="COLOR: rgb(0,128,0)">-->
    <bean id="openSessionInViewInterceptor"
             class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="interceptors" ref="openSessionInViewInterceptor"/>
        <property name="mappings">
            <props>
               ......
            </props>
        </property>
    </bean>
2. Filter
<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> 
第2种解决方法:

Hibernate.initialize()强制加载关联对象(initialize为延迟加载的集合)

分享到:
评论

相关推荐

    Git-2.21.0-64-bit.zip

    "checking out paths out of the index and/or a tree-ish to work on advancing the current history" out of the single "git checkout" command. * "git branch --list" learned to always output the ...

    Packt.Mastering.Csharp.and.NET.Programming

    - **Context**: Context in computing refers to the specific environment or settings under which a particular operation occurs. This includes variables, registers, and other resources that the running ...

    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 ...

    Free Hex Control

    / Errrrr...Actually, I hesitate to release the source code of this control, / Because when I checked after completion, I found that it's really ugly! Putting all / graphic codes in the CDraw class ...

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

    npm install --save react-lazily-render 用法 () import React from 'react' ; import LazilyRender from 'react-lazily-render' ; ...lots of content... &lt; LazilyRender xss=removed&gt; } content = { ...

    JavaScript Concurrency pdf 无水印 0分

    Apply the core principles of concurrency to both browser and server side development Explore the latest tools and techniques at the forefront of concurrent programming, including JavaScript promises, ...

    2019_2020学年高中英语Unit4BodyLanguageperiod2WarmingUp&Reading2课件新人教版必

    - 完成句子练习可以帮助巩固词汇的应用,如:"The young man rose lazily and was not willing to greet us."(年轻人懒洋洋地起身,并不愿意向我们打招呼。),"This appointment was greeted with relief."(这个...

    广东省深圳市文汇中学2015届九年级英语上学期第7周周末作业(答案不全) 牛津深圳版

    `lazily`:金鱼慵懒地(lazily)成群结队地在水面下游动。 - 15. `coats`:他们给前门刷了三层(coats)油漆。 2. **关键短语或词组**: - 1. `come along`:一起来,过来。 - 2. `a novel called…`:一本叫做...

    spring-reference.pdf

    - **Easier configuration of declarative transactions in XML**: 在XML配置中简化了声明式事务的配置。 - **JPA**: 支持Java Persistence API,为持久化层提供了一致性的API接口。 - **Asynchronous JMS**: 提供...

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

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

    Android代码-Kodein-DI

    KOtlin DEpendency INjection Kodein is a very simple and yet very useful dependency ...Easily bind classes or interfaces to their instance or provider Easily debug your dependency bindings and rec

    springframework.5.0.12.RELEASE

    Exporting a lazily initialized bean (which implements SelfNaming and is annotated with ManagedResource annotation) gives IllegalStateException [SPR-17592] #22124 MockHttpServletRequest changes Accept-...

    Pandas Cookbook 2017 pdf 2分

    Table of Contents Preface What this book covers What you need for this book Running a Jupyter Notebook Who this book is for How to get the most out of this book Conventions Assumptions for every ...

    初中英语第六册(北京版)

    (In this paper, by the statistical analysis of the path, source and moving velocity of the cold air, blocking high on the north of Baikal and Yakutsk was shown to be the main system of influence.)。...

    新概念英语第一册第109-110课PPT课件.pptx

    - "more or less"意为"或多或少","no less than"表示"绝不少于,多达"。 - "teaspoonful"指的是"一满茶匙"的量,如"a teaspoonful of tea"。 - "pity"表示"遗憾",感叹句中可使用"What a pity!"表达惊讶或惋惜。...

    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, ...

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

    获取所有文件 具有延迟同步和异步迭代器支持的出色的快速递归目录搜寻器。 安装 支持Node.js版本10及更高版本。 $ npm i get-all-files ...path/to/dir/or/file` ) ) { // Could break early on some condition a

    广东省广州市2018年中考英语学科模拟题十六20180709274

    3. 冠词:"a good worker",不定冠词a用于表示泛指,此处表示“一个好工人”。 4. 形容词与过去分词的区别:"Although surprised",形容词surprised用于表示人的感受。 5. 连词:"because he really needed help",...

    spring-reference

    - **Lazily-Instantiated Beans**:介绍了延迟加载Bean的概念,即在真正需要的时候才加载Bean实例。 - **Autowiring Collaborators**:通过autowire属性自动装配Bean之间的依赖关系。 - **Checking for Dependencies...

Global site tag (gtag.js) - Google Analytics