解决could not initialize proxy - no Session

3人阅读 评论(0) 收藏 举报
分类:

问题描述:


org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.zhai.model.sys.Tresource.resources, could not initialize proxy - no Session

    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575)
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:214)
    at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:554)
    at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:142)
    at org.hibernate.collection.internal.PersistentSet.iterator(PersistentSet.java:180)
    at com.alibaba.fastjson.serializer.CollectionSerializer.write(CollectionSerializer.java:68)
    at com.alibaba.fastjson.serializer.JSONSerializer.writeWithFieldName(JSONSerializer.java:390)
    at Serializer_1.write1(Unknown Source)
    at Serializer_1.write(Unknown Source)
    at com.alibaba.fastjson.serializer.JSONSerializer.writeWithFieldName(JSONSerializer.java:390)
    at Serializer_1.write1(Unknown Source)
    at Serializer_1.write(Unknown Source)
    at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:361)
    at com.alibaba.fastjson.JSON.toJSONString(JSON.java:393)
    at com.alibaba.fastjson.JSON.toJSONString(JSON.java:381)
    at com.zhai.service.ResourceTest.getTreemenu(ResourceTest.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

导致出现该错误的代码:@ManyToMany(fetch = FetchType.LAZY) 采用了懒加载;


    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "sys_role_resource", joinColumns = { @JoinColumn(name = "resource_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "role_id", nullable = false, updatable = false) })
    @OrderBy("id ASC")
    public Set<Trole> getRoles() {
        return roles;
    }

解决方法:
1、把lazy设成false。这个是最简单的办法,个人认为也是比较笨的方法。因为这是在用效率作为代价。

2、使用OpenSessionInViewFilter。这种方法是将session交给servlet filter来管理,每当一个请求来之后就会开

启一个session,只有当响应结束后才会关闭。如下:
在web.xml配置文件中加上该配置

<filter-name>hibernateFilter</filter-name>   
     <filter-class>  org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class>   
</filter   
<filter-mapping>   
     <filter-name>hibernateFilter</filter-name>   
     <url-pattern>/*</url-pattern>   
</filter-mapping> 

3、若是单纯的junit 测试的就只需加上 @Trantsactional

    @Autowired
    private ResourceServiceImpl resourceService;
    @Test
    @Transactional
    public void getTreemenu() {
        List<Tresource> l = null;
        l = resourceService.getTremenu(false);
        for (Tresource t : l) {
            System.out.println(JSONArray.toJSONString(t));
        }
    }
}
查看评论

懒加载异常说明及解决:could not initialize proxy-no Session

【问题介绍】 在项目中遇到这样的错误反馈 别的没有看懂,但是被红线框着的地方肯定能看懂,没有session值。这是懒加载异常的出现的情况。 【问题出现原因】 原因:一个请求来的...
  • u012581322
  • u012581322
  • 2017-03-31 20:07:52
  • 1320

spring-boot拦截器中出现could not initialize proxy - no Session完美解决方案

spring-boot拦截器中出现could not initialize proxy - no Session完美解决方案
  • zhangjq520
  • zhangjq520
  • 2017-01-06 11:11:48
  • 1802

解决Hibernate:could not initialize proxy - no Session

异常: org.hibernate.LazyInitializationException: could not initialize proxy - no Session at org.hibe...
  • u010167086
  • u010167086
  • 2013-11-29 21:04:51
  • 14269

JPA中的could not initialize proxy - no Session异常分析与解决

引言: JPA是一种非常流行和常用的持久化框架标准,其下可以对接若干种不同的实现,在不同的父子表管理中,经常会碰到no Session的问题,该如何解决呢?...
  • blueheart20
  • blueheart20
  • 2016-10-24 17:03:32
  • 9067

spring-boot 完美解决:could not initialize proxy - no Session

spring-boot 完美解决:could not initialize proxy - no Session
  • zhangjq520
  • zhangjq520
  • 2016-12-19 15:35:15
  • 4318

Hibernate3中could not initialize proxy - no Session的解决办法

最近自己在进行多表操作,出现了
  • wangdianyong
  • wangdianyong
  • 2014-05-26 13:55:28
  • 856

junit test中解决could not initialize proxy - no Session

项目中把session的管理交给了sping,靠着openEntityManagerInViewFilter逍遥。今天写junit测试的时候出现了许久未见的no session问题。最后找到了解决方案...
  • fo11ower
  • fo11ower
  • 2017-03-11 00:05:23
  • 1077

JPA之"could not initialize proxy - no Session"的异常解决

引言: 在使用JPA中经常会碰到各类问题,这里列出解决no Session问题的方法。...
  • blueheart20
  • blueheart20
  • 2016-09-08 16:23:55
  • 3363

could not initialize proxy - no Session 异常情况探究

近期跟着“尚学堂”马士兵的spring教学视频复习了一番,学到45课的时候,得知了这样一个据说经典的异常: org.hibernate.LazyInitializationException: cou...
  • agxy08xx1mxx
  • agxy08xx1mxx
  • 2013-08-28 11:35:32
  • 1564

错误记录(八)could not initialize proxy - no Session

在SSH整合项目中出现了如下错误信息: org.hibernate.LazyInitializationException:could not initialize proxy - no Sessio...
  • weixin_36380516
  • weixin_36380516
  • 2017-05-31 16:31:45
  • 5500
    个人资料
    持之以恒
    等级:
    访问量: 930
    积分: 114
    排名: 122万+
    文章存档