---
title: "[#0x0048] hibernate延迟加载的错误"
author: "Aoyi"
date: "2010-08-28T14:45:59Z"
tags: []
categories: ["企业架构"]
slug: "0x0048-hibernate"
draft: false
lastmod: "2010-08-28T14:45:59Z"
id: 749666
url: "https://aoyi.iteye.com/blog/749666"
---

异常信息：failed to lazily initialize a collection of role: ＜容器类对象＞, no session or session was closed

 

　　这个问题一般出现在@OneToMany的情况下，解决的方法：  
1\. 如果是xml配置，在hbm文件中的对应属性上配置lazy=“false”  
2\. 如果是用annotation，则配置如下  
@OneToMany(  
　　targetEntity = CourseAuthorizationItem.class,  
　　cascade = { CascadeType.PERSIST, CascadeType.MERGE },  
　　mappedBy = "xxx", fetch = FetchType.EAGER  
)  
将fetch类型设置成直接获取