Could not lazily initialize role collection

Hi everyone, I am creating a GRAILS application with m: m db ratio. When I try to show the records, the well-known "failed to lazily initialize the role collection ... session or session was not closed", an error is displayed.

One class:

class Hazzard{

static hasMany = [warning:Warning]

static constraints = {
    text(size:1..5000)
}

    String name
    String text
    String toxicity
}

other:

class Warning{

static hasMany = [hazzard:Hazzard]
static belongsTo = Hazzard

static constraints = {
    text(size:1..5000)
}

    String code
    String text   
}

Hazzard / shows that the following code works fine

<g:each in="${hazzardInstance.warning}" var="p">
<li><g:link controller="Warning" action="show" id="${p.id}">${p?.encodeAsHTML()}</g:link></li>
</g:each>

but the following code is shown on other pages:

<g:set var="haz" value="${Hazzard.get(params.id)}" />
<h1>${haz.name}</h1>
<p>${haz.text}</p>
<h1>Toxiciteit</h1>
<p>${haz.toxicity}</p>
<br/>
<h1>Gevaren(H) en voorzorgen(P)</h1>
<g:each in="${haz.warning}" var="p"> --> This is where the error pops-up
  ${p.text}
</g:each>

Any tips on where this fails?

+3
source share
1 answer

A more favorable approach to what you are trying to do is to execute getin the controller and pass the found domain object to the view for rendering. Sort of:

// MyController.groovy
class MyController {
    def myAction = {
        def haz = Hazzard.get(params.id)
        render(view: 'myview', model: [hazzardInstance: haz])
    }
}

// my/myview.gsp (the view from your second GSP code block)
<h1>${hazzardInstance?.name.encodeAsHTML()}</h1>
...
<h1>Gevaren(H) en voorzorgen(P)</h1>
<g:each in="${hazzardInstance?.warning}" var="p">...</g:each>

GORM , , , Grails. , .

+2

Source: https://habr.com/ru/post/1788138/


All Articles