atul
atul

Reputation: 148

failed to lazily initialize a collection of role In Grails

I got struck here. There is a parent Table like below.

     class Parent{
        static hasMany = [child: Child];
        static Parent findByParent(Parent parent){
        def c = Parent.createCriteria()
        def result = c.get {
        child{
             idEq(parent.id)
        }
       }
      }
     }

    class Child {

    }

Below is controller action.

    def save(Child child){
      def orderList;
      if(parent){
        orderList = parent?.child(); // here i am getting exception. failed to lazily initialize 
                                      //  a collection of role: Parent.child, no session or session  
                                        //was closed. Stacktrace follows:
      }
    }      

So how to handle this situation.Or is there any Annotations to Eager initialization.

Upvotes: 1

Views: 2462

Answers (1)

Madhu Bose
Madhu Bose

Reputation: 371

For eager fetching you can explicitly set lazy as false in your Parent domain .Try something like this

static mapping = {
    child lazy: false
}

Hope it helps...

Upvotes: 4

Related Questions