Assume that we have such domain hierarchy:
class Category {
...
static hasMany = [groups: Group]
...
}
class Group {
...
static belongsTo = [category: Category]
static hasMany = [items: Item]
...
}
class Item {
...
Integer value
static belongsTo = [group: Group]
...
}
The field value in the Item class should be unique within one Category. How can we achieve this?
I tried to write a custom validator using criteria, but finally I got a "don't flush the Session after an exception occurs" Hibernate error. Is there a better way to check this constraint?
Hmm maybe
class Item {
...
Integer value
static belongsTo = [group: Group, category: Category]
static constraints = {group value: 'category'}
...
}
Also you could add custom validator to check if parent group belongs to the same category.
Did not test it.