`

could not load an entity

阅读更多
1.could not load an entity  (get)
2.23:31:32,468 ERROR LazyInitializationException:19 - could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed  (load)

Lazy问题可以通过加opensessioninviewfilter解决
在web.xml中加上
<filter>
<filter-name>openSessionInViewFilter </filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter </filter-name>
<url-pattern>*.do </url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>openSessionInViewFilter </filter-name>
<url-pattern>*.jsp </url-pattern>
</filter-mapping>

经过调试是 LAZY的问题.
我的解决办法是:
<many-to-one>里面的属性lazy="false"

<set>里面的属性lazy="true"
<one-to-many>
</set>

有不对的地方,请大家补充

其次关于load取数据出现问题是因为取数据的表有级联关系,在我们分析此问题之前,我们先要明白hibernate中什么是load,什么是get,它们两者之间有什么区别呢??
只要你在以往的开发过程中稍加注意,不难发现,假如有两张表,他们为一对多关系,现在开始查询,如果使用get,那么查询的结果将会是把2张表的数据全部持久化到内存中(如果关系比较深你还打算用它吗?),如果是采用load的方式,那么将只会是把其中一张表(A表)的数据加载到内存中,而另一张表(B表)只存在一个标识,原则上是等页面显示时,如果只要显示A表数据,那么程序直接内存中找A表数据,如果还要显示B表数据,那么就要通过内存中B表的标识重新查找数据库,此时,问题出来了,假如你在DAO中,session.load之后你结束了session的生命周期,那么在页面要显示B表数据,而B表在内存中只是一个标识啊,你的session又处于关闭状态,它无法找啊,这就会出现上面你遇到的那个问题,因此,假如我们不关闭session,页面可以正常显示2张表的数据了,但是问题又来了,如果不关闭session,那么对象将一直以持久态的形式保存在内存中,留下的隐患难以想象(比如该对象容易数据混乱等等),千万不要说session.clear之内的东西也可以把对象变游离态之内的话啊,如果你彻底用session.clear取代session.close的话,嘿嘿,自己去尝试吧,当问题显现出来的时候痛苦吧^_^!好了,我还是说下解决方法吧~最好的方法就是将DAO中的session与事物全部抽取出来做成一个hibernate过滤器,这样既能解决你上面的问题,又能大大减少DAO中的代码...好处很多的哦,使用一次之后就容易上瘾
分享到:
评论

相关推荐

    h2-lob-issue:H2 1.4“找不到Lob”问题的可重现单元测试

    H2 1.4 "Lob not found" 问题 - 在 1.4.183 中修复!...org.hibernate.exception.GenericJDBCException: could not load an entity: [de.bwaldvogel.LobEntity#1] at org.hibernate.exception.SQLStateConverter.handl

    Chinese Entity Linking Comprehensive

    equivalence class clusters for NIL entities (those that could not be linked to an entity in the knowledge base), and entity type information for each of the queries. The data included in this ...

    CFS调度器(4)-PELT(per entity load tracking).pdf

    PELT(Per Entity Load Tracking)算法是在Linux 3.8版本中引入的,它解决了在Linux 3.8之前CFS调度器存在的问题。 在引入PELT之前,CFS以运行队列(runqueue)为基础来跟踪负载。这意味着系统将所有可运行的任务...

    Entity Framework 6 Power Tools Community Edition

    When right-clicking on an Entity Data Model .edmx file, the following context menu function is available: Generate Views. If you are looking for Reverse Engineeering tools, I recommend using the EF ...

    MyBatis_3.0.2_patch.docx

    当遇到“Could not find SQL statement to include with refid 'SAD02.SAD02_COL'”这样的异常时,通常表示MyBatis无法找到指定的SQL语句引用。这可能由以下几个原因引起: 1. **XML配置文件未正确加载**:确保对应...

    springMVC-HttpEntity(ResponseEntity)demo

    在Spring MVC框架中,HttpEntity和ResponseEntity是两个非常重要的概念,它们主要用于处理HTTP请求和响应。本项目“springMVC-HttpEntity(ResponseEntity)demo”是一个实战演示,展示了如何在Spring MVC应用中使用...

    解决返回JSON报错:HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowE

    然而,当出现"HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError)"这样的错误时,意味着在序列化过程中遇到了无限递归的问题。这个问题通常是由于对象之间的引用循环...

    VHDL.rar_entity load_out

    ENTITY FREQ_T IS PORT(CLK:IN STD_LOGIC FREQ_EN:OUT STD_LOGIC CNT_CLR:OUT STD_LOGIC Load:OUT STD_LOGIC) END FREQ_T

    System.Data.Entity

    《深入理解System.Data.Entity》 System.Data.Entity是.NET框架中一个关键的部分,它构成了Entity Framework的核心,这是一个强大的对象关系映射(ORM)框架,用于简化数据库操作。ORM允许开发人员使用面向对象的...

    Programming Entity Framework DbContext

    在本篇详细知识点讲解中,将基于给定文件信息,深入探讨Entity Framework(实体框架)中Code First方法的相关知识点。根据文件标题《Programming Entity Framework DbContext》和描述,该文件应该是关于Entity ...

    Entity.Framework.Tutorial.2nd.Edition.1783550015

    Create an Entity Data Model using the ADO.NET Entity Data Model Designer and the EdmGen tool Extend existing Entity Types to create your own Entity Types Write programs against the Entity Data Model ...

    EntityFramework.zip

    Entity Framework (EF) 是微软提供的一款强大的对象关系映射(ORM)框架,它允许.NET开发者使用面向对象的编程方式来操作数据库,而无需关注底层的SQL语句。在这个"EntityFramework.zip"压缩包中,我们可以预想包含...

    Mastering Entity Framework(PACKT,2015)

    Data access is an integral part of any software application. Entity Framework provides a model-based system that makes data access effortless for developers by freeing you from writing similar data ...

    EF(EntityFramework) 插入或更新数据报错的解决方法

    报错信息:Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See ...

    Entity Framework 6 Recipes

    Entity Framework 6 Recipes Entity Framework 6 Recipes

    Entity Framework Core Cookbook - Second Edition 2016

    Ricardo Peres is a Portuguese developer, blogger, and occasionally an e-book author. He has more than 17 years of experience in software development, using technologies such as C/C++, Java, JavaScript...

    Entity Framework官方中文教程

    Entity Framework(EF)是微软提供的一个对象关系映射(O/RM)框架,它简化了.NET开发人员访问数据库的代码编写,无需手动编写大量数据访问代码。EF允许开发者通过.NET对象模型来操作数据库。Entity Framework Core...

    Entity Framework 4.0 and Web Forms

    Entity Framework 4.0和*** Web Forms是微软公司推出的用于构建Web应用程序的技术,这本书主要讲述了如何使用Entity Framework 4.0在*** Web Forms应用程序中实现数据的显示和编辑。 Entity Framework是微软的.NET...

    Entity Framework 4 In Action

    ### Entity Framework 4 In Action:全面解析与应用实践 #### 一、书籍概述与背景介绍 《Entity Framework 4 In Action》是一本深入探讨Entity Framework 4(简称EF4)的权威指南,由Stefano Mostarda、Marco De ...

    EntityFramework.SqlServer_EntityFramework_

    Entity Framework (EF) 是微软提供的一款强大的对象关系映射(ORM)框架,它允许开发者使用.NET语言(如C#或VB.NET)来操作数据库,而无需编写大量的SQL语句。在.NET开发中,EF极大地提高了开发效率,因为它将数据...

Global site tag (gtag.js) - Google Analytics