Mikhail Krutov
Mikhail Krutov

Reputation: 712

Class belonging to either of two?

How should I solve this relation between objects:

Object1 & Object2 are similiar "parent objects"

Object3 may be a child to Object1 or Object2.

How is this relation called and how should I code this in Grails?

Upvotes: 0

Views: 39

Answers (1)

Mikhail Krutov
Mikhail Krutov

Reputation: 712

as I've found after asking this question:

I can have multiple belongsTo, just need to mark them nullable & check that only 1 of them are actually null. In my case,:

static belongsTo = [obj1: Object1, obj2: Object2];
static constraints = {
    obj1(nullable: true, validator: {field, inst -> inst.obj2 || field})
    obj2(nullable: true)
}

Upvotes: 1

Related Questions