`

整理问题:Session was closed

阅读更多
引用

答案一:

当<class>元素或者<set>元素的lazy属性为true时,load() or get() or find()加载这些对象时,Hibernate不会马上产生任何select语句,只是产生一个Obj代理类实例,只有在session没有关闭的情况下运行Obj.getXxx()时才会执行select语句从数据库加载对象,如果没有运行任何Obj.getXxx()方法,而session已经关闭,Obj已成游离状态,此时再运行Obj.getXxx()方法,Hibernate就会抛出"Could not initialize proxy - the owning Session was closeed"的异常,是说Obj代理类实例无法被初始化。然而想在Session关闭之前不调用Obj.getXxx()方法而关闭Session之后又要用,此时只要在Session关闭之前调用Hibernate.initialize(Obj)或者Hibernate.initialize(Obj.getXxx())即可,net.sf.hibernate.Hibernate类的initialize()静态方法用于在Session范围内显示初始化代理类实例。

答案二:

抛出这个错误的主要原因我觉得并不是由于Session没有关闭造成的,这种问题我也遇到过,不过原因说不是很清楚,我用的hibernate3.0的lazy默认好像是true这个原因是没有初始化造成的,可以在session.close()之前把customer的一个属性输出来试一试,或者用Hibernate.initialize初始化,在或者干脆把lazy改为false行了。
分享到:
评论
1 楼 liujun999999 2007-06-05  
太谢谢楼主了,正为这个问题发愁呢

相关推荐

    SecureCRT+SecureFX v7.1 included Keygen

    官方2013年5月2号更新的SecureCRT+SecureFX v... - Mac/Linux: If the toolbar was displayed, when SecureFX was closed and restarted, the application window was shorter that the previous time SecureFX ran.

    遇到的问题1

    2. **Hibernate的session问题**: "no session or session was closed"提示可能是因为在Hibernate配置文件中,外键的lazy属性设置为"true"。若需要立即加载关联对象,应将其改为"false"。 3. **Struts的checkbox使用...

    软件工程实习日志

    - **技术难题**:实习生在项目开发过程中会遇到各种技术难题,例如数据库连接失败(`failedtolazilyinitializeacollection: nosession or session was closed`)等问题。 - **解决方案探索**:通过查阅文档、搜索...

    解决virtualbox不能为虚拟电脑打开一个新任务。

    对于这个问题,我们需要深入理解VirtualBox的工作原理,并采取相应的解决方案来修复。 首先,VirtualBox是一个开源的虚拟化平台,它允许用户在单一的物理机器上运行多个操作系统实例。当出现“不能为虚拟电脑打开一...

    Hibernate映射导致的几个异常

    Could not initialize proxy - the owning Session was closed 这是Hibernate懒加载机制中的常见异常,当尝试访问一个已被关闭的Session中的懒加载属性时触发。例如,如果Session在使用`setFetchMode(FetchMode....

    Hibernate延迟加载以及利用Spring

    Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed ``` - **日志记录**:为了更好地诊断问题,可以使用log4j等工具进行日志...

    Sublime Text 3 (Build 3143)

    Fixed flash-of-white that could occur when the auto complete window is closed Disable scroll animation when animation_enabled is false in settings Files can now be renamed when only the case has ...

    解析ActiveMQ的使用说明总结

    - **网络异常与"The Session is closed"错误**:为避免因网络问题导致的断开连接,可设置`maxReconnectDelay`,如`maxReconnectDelay=10000`,以控制最大重连间隔。 3. **发送模式** - **异步发送**:通过添加`...

    微软内部资料-SQL性能优化3

    To make use of either more or less strict isolation levels in applications, locking can be customized for an entire session by setting the isolation level of the session with the SET TRANSACTION ...

    计算机网络第六版答案

    Computer Networking: A Top-Down Approach, 6th Edition Solutions to Review Questions and Problems Version Date: May 2012 ...This document contains the solutions to review questions and problems for...

    微软内部资料-SQL性能优化2

    A 32-bit process is normally limited to addressing 2 gigabytes (GB) of memory, or 3 GB if the system was booted using the /3G boot switch even if there is more physical memory available. By leveraging...

    数位板压力测试

    This specification was developed in response to a perceived need for a standardized programming inter-face to digitizing tablets, three dimensional position sensors, and other pointing devices by a ...

    Delphi7.1 Update

    visit the Borland Registered User web site to obtain a localized readme file that may contain important late- breaking information not included in this readme file.IMPORTANT: Delphi must be closed ...

Global site tag (gtag.js) - Google Analytics