Validation Grails:复制品&;唯一约束验证
好的,这是我应用程序中的精简版本 艺术家领域:Validation Grails:复制品&;唯一约束验证,validation,grails,gorm,Validation,Grails,Gorm,好的,这是我应用程序中的精简版本 艺术家领域: class Artist { String name Date lastMined def artistService static transients = ['artistService'] static hasMany = [events: Event] static constraints = { name(unique: true) lastMined(
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)
}
}
艺人服务:
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)
}
}
}
类艺人服务{
def结果=[
[
姓名:“姓名”,
详情:“详情”,
国家:“国家”,
城镇:“城镇”,
地点:“地点”,
url:“url”,
日期:“日期”
]
]
def mine(艺术家){
结果Grails的哪个版本?我刚刚在Grails 1.3.1控制台中测试了这段代码:
new Book(title: "Misery", author: "Stephen King").save()
new Book(title: "The Shining", author: "Stephen King").save()
new Book(title: "Colossus", author: "Niall Ferguson").save(flush: true)
def b = new Book(title: "Colossus", author: "Stephen King")
assert !b.validate()
println b.errors
断言通过,最后一行生成如下输出:
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'Book' on field 'title': rejected value [Colossus]; codes [Book.title.unique.error.Book.title,...
也许这是一个现已修复的bug?哪个版本的Grails?我刚刚在Grails 1.3.1控制台中测试了这段代码:
new Book(title: "Misery", author: "Stephen King").save()
new Book(title: "The Shining", author: "Stephen King").save()
new Book(title: "Colossus", author: "Niall Ferguson").save(flush: true)
def b = new Book(title: "Colossus", author: "Stephen King")
assert !b.validate()
println b.errors
断言通过,最后一行生成如下输出:
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'Book' on field 'title': rejected value [Colossus]; codes [Book.title.unique.error.Book.title,...
也许这是一个现已修复的bug?您应该将artist.addToEvents(event)
替换为artist.addToEvents(event).save()
,它将正常工作。
除非您没有调用save()方法,否则验证将不会考虑新创建的事件。您应该将artist.addToEvents(event)
替换为artist.addToEvents(event).save()
,它将起作用。
除非您没有调用save()方法,否则验证将不会考虑新创建的事件我不确定重复逻辑失败的原因,但您的代码中有一个严重的错误。服务是单例的,但您在该服务中共享了状态-“结果”列表。当您是应用程序的唯一用户时(在开发模式下),它可以正常工作但当您有多个并发访问此方法时,将失败。在我的实际应用程序中,结果在方法范围内,但感谢您提出它,可能会在将来有所帮助。我不确定重复逻辑为什么会失败,但您的代码中有一个严重的错误。服务是单例的,但您在此服务中有共享状态-“结果”当您是应用程序的唯一用户时(在开发模式下),它将正常工作但当您有多个并发访问此方法时将失败。在我的实际应用程序中,结果在方法范围内,但感谢您提出它,可能会在将来有所帮助在测试应用程序上检查它-在我的实际应用程序上效果很好-仍会引发重复条目异常。仅保存(flush:true)消除问题:|在测试应用程序上检查它-在我的实际应用程序上像符咒一样工作-仍然抛出重复条目异常。只有保存(flush:true)才能消除问题:|