0

I have two domain classes, A and B. B class belongs to A class. I want that, when A object is created, a B object is automatically created too and added into the related db table (with the id value of the A object). Is it possible? How can I do it?

I've tried doing the following (in A controller):

def b = new B();

    b.a = aInstance;

    if(!b.save(flush: true)){
        flash.message = "error"
        return
    }

but b.save() always fails...

EDIT: it seems that the problem is that some field is required. the class B is as follows:

class B {

int field1;
int field2;
String field3;

static belongsTo = [anstances:A]
static constraints = {
}

}

Why do the fields field1 and field2 are required?

EDIT2: I've changed type of int fields to String. Now they are not required, but save() returns null and i see the "error" label

4

1 回答 1

1

根据您的编辑,保存问题是因为您没有对属性指定约束,并且字段可以为空:默认情况下为 false。从文档中:

nullable:允许将属性设置为 null -默认为 false

于 2013-10-31T14:24:16.857 回答