hibernate - Grails Domain - Child with multiple parent cascade behavior -
i have following domain classes
class child { static belongsto = [parent1: parent1, parent2: parent2] static constraints = { parent1(nullable: true) parent2(nullable: true) } } class parent1 { child singlechild } class parent2 { static hasmany = [children: child] static mappedby = [children: 'parent2'] static mapping = { children cascade: "all, all-delete-orphan" } }
child belongs parent1 and/or parent2.
parent1 has onetoone relation child , parent2 has onetomany relation child.
problem: if delete parent1, see child belongs both parent1 , parent2 gets deleted.
question: there way not automatically delete child if belongs parent2?
i able overcome issue doing following:
for cascade save , update
1) remove [parent1: parent] belongsto in child class
2) add cascade: "save-update" parent1 class.
this modified child , parent class. no change parent2
class child { static belongsto = [parent2: parent2] static constraints = { parent2(nullable: true) } } class parent1 { child singlechild static mapping = { singlechild cascade: "save-update" } }
when parent1 deleted, check if singlechild has parent2. if not, delete singlechild. here code
if(parent1.singlechild.parent2 == null) { parent1.singlechild.delete() }
Comments
Post a Comment