gpt4 book ai didi

hibernate - Grails GORM : could not initialize proxy - no Session

转载 作者:行者123 更新时间:2023-12-04 01:44:45 26 4
gpt4 key购买 nike

我有一个具有以下结构的方法:

public void run(){
...
for (...) { //this part works correct

User.withTransaction {
User user = User.findByUsername(...);

Position pos = Position.findByName(...)
if(pos){ ...
} else { ...
try{
pos.save(flush:true);
user.position = pos;
} catch (Exception e){ ... }
}
...
try{
user.save(flush:true, failOnError: true);
} catch (Exception e){ ... }
}
}
//this part doesn't work
User.findAll().each {
...
if (...){
User.withTransaction{
...
//here the operation fails with
//org.hibernate.LazyInitializationException:
//could not initialize proxy - no Session
if (!userDetailsMap.containsKey(it.username)
&& it.userStatus != blocked){
it.userStatus = blocked
it.save(flush:true)
}
}
}
}
}

我在这里遇到的异常(exception)是 org.hibernate.LazyInitializationException: could not initialize proxy - no Session在我的代码的第二部分。这里 userStatus field 是对不同域类的引用。

我尝试添加 it.refresh()Hibernate.initialize(it)检查属性之前的代码,但没有用。我在这里做错了什么?

upd:我试着调用 it.attach检查属性之前的方法,但在方法调用之后的值 it.attachedfalse .

最佳答案

我认为您没有做任何“错误”的事情,只是该对象与 hibernate session 分离。我会尝试的几件事:

  • 像这样将对象附加回 hibernate session : object.attach() ( http://grails.org/doc/2.2.1/ref/Domain%20Classes/attach.html )
  • 使两个域类之间的关联非惰性 ( http://grails.org/doc/2.2.x/ref/Database%20Mapping/lazy.html )
  • 关于hibernate - Grails GORM : could not initialize proxy - no Session,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15947304/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com