Cannot attach an object to Hibernate session

落花浮王杯 提交于 2019-12-12 14:00:18

问题


I have 3 domains with these relationships:

A hasMany [bs: B] B belongsTo [c: C] C

Inside a webflow I do this (simplified version):

flow.a = new A(stuff:stuff)
flow.a.addToBs(new B(c:C.get(1)))
flow.a.addToBs(new B(c:C.get(2)))
flow.a.addToBs(new B(c:C.get(3)))

and then I try to show all this information on a gsp page:

<g:each in="${a.bs}" var="b">
    ${b.c.someProperty}
</g:each>

This is where I get LazyInitializationException. I think I understand why (webflow serializes flow scope) but when I try to attach() all the instances of C they don't get attached:

flow.a.bs.each {
    it.c.isAttached() // returns false
    it.c.attach()
    it.c.isAttached() // returns false
}

Why is this? No error messages I can see... Is there any better way of doing this (I'm thinking of setting lazy:false for these relationships)?

来源:https://stackoverflow.com/questions/15671456/cannot-attach-an-object-to-hibernate-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!