class A
{
String name
static hasMany=[bs:B] //by default bs are Set. no need of explicit declaration
}
Class B
{
String name
static belongsTo = A //when delete a it's b also will get deleted
}
B b1=new B(name:'bname1')
B b2=new B(name:'bname11')
B b3=new B(name:'bname2')
A a=new A(name:'aname1')
A a2=new A(name:'aname2')
a.save(flush:true)
a2.save(flush:true)
a.addToBs(b1)
a.addToBs(b2)
a2.addToBs(b3)
a.save(flush:true)
a2.save(flush:true)
No need of save the instance of b
. When we add the b
instance into the collection of b's
on a
, it will automatically save the instance of b
when we save the instance of a
.