Cannot attach an object to Hibernate session

2019-07-04 06:51发布

问题:

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)?