確定被剝離下來的我有什麼版本在我的應用程序的Grails:重複和獨特的約束驗證這裏
藝術家域:
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
我不確定爲什麼重複邏輯失敗,但是在代碼中存在嚴重錯誤。服務是單身人士,但您已在此服務中共享狀態 - 「結果」列表。 當你是你的應用程序的唯一用戶(在開發模式下)時它會工作正常,但當你有多個併發訪問這個方法時會失敗。 – 2010-05-24 03:09:33
在我的實際應用程序結果是在一個方法範圍內,但感謝提出,可能在未來幫助 – rukoche 2010-05-24 12:16:07