ОК, здесь урезанная версия того, что у меня в приложении
Домен исполнителя:
class Artist {
String name
Date lastMined
def artistService
static transients = ['artistService']
static hasMany = [events: Event]
static constraints = {
name(unique: true)
lastMined(nullable: true)
}
def mine() {
artistService.mine(this)
}
}
Домен событий:
class Event {
String name
String details
String country
String town
String place
String url
String date
static belongsTo = [Artist]
static hasMany = [artists: Artist]
static constraints = {
name(unique: true)
url(unique: true)
}
}
ArtistService:
class ArtistService {
def results = [
[
name:"name",
details:"details",
country:"country",
town:"town",
place:"place",
url:"url",
date:"date"
]
]
def mine(Artist artist) {
results << results[0] // now we have a duplicate
results.each {
def event = new Event(it)
if (event.validate()) {
if (artist.events.find{ it.name == event.name }) {
log.info "grrr! valid duplicate name: ${event.name}"
}
artist.addToEvents(event)
}
}
artist.lastMined = new Date()
if (artist.events) {
artist.save(flush: true)
}
}
}
В теории event.validate () должен возвращать false, и событие не будет добавлено к исполнителю, но это не .., что приводит к исключению БД на artist.save ()
Хотя я заметил, что если повторяющееся событие сохраняется, сначала все работает как задумано. Это ошибка или особенность? : P