首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Exception >

LazyInitializationException: could not initialize proxy - the owning Session was

2014-01-26 
下面这个方法是没有错误的,可是用注释的语句就会出现LazyInitializationException: could not initialize p

下面这个方法是没有错误的,可是用注释的语句就会出现LazyInitializationException: could not initialize proxy - the owning Session was closed
为什么呢?
public Article getArticleById(Long id) {
Session session = getSession();
Transaction tx = session.beginTransaction();
Article article = (Article) session.load(Article.class, id);
article.getArticleTitle();
tx.commit();
session.close();
return article;
/*Article a=(Article)getObject(Article.class, id);
System.out.println(a.getArticleTitle());
return a;*/
}

/*Article a=(Article)getObject(Article.class, id);来源于:
public Object getObject(Class entityClass, Long id) {
Session session = getSession();
Transaction tx = session.beginTransaction();
Object o = session.load(Article.class, id);
System.out.println(o.hashCode());
tx.commit();
session.close();
return o;
}

------解决方法--------------------------------------------------------
这个是多对一时没有家载而关了session后要显示出现的错误

你的Object o = session.load(Article.class, id);改成get或许可以
你看看load和get的方法吧
 

        

热点排行