【发布时间】:2011-05-31 19:53:00
【问题描述】:
交换唯一属性值后,Grails 验证失败
您好,我正在尝试创建一个界面,用户可以在其中创建一些自定义枚举以及不同语言的翻译。例如,用户可以创建枚举“Movie Genre”。对于这个枚举,可能存在一个枚举值“喜剧”,其中可能存在一个或多个针对多种语言的枚举值翻译。
由于特定语言只能有一个翻译,因此我向枚举值翻译域类添加了唯一约束。这些是我现在的域类:
class Enumeration {
String label
List<EnumerationValue> enumerationValues = new ArrayList<EnumerationValue>()
static hasMany = [ enumerationValues: EnumerationValue ]
static constraints = {
label(nullable: false, blank: false)
enumerationValues(nullable: true)
}
}
class EnumerationValue {
String label
List<EnumerationValueTranslation> enumerationValueTranslations = new ArrayList<EnumerationValueTranslation>()
static belongsTo = [ enumeration: Enumeration ]
static hasMany = [ enumerationValueTranslations: EnumerationValueTranslation ]
static constraints = {
label(nullable: false, blank: false, unique: 'enumeration')
enumeration(nullable: false)
enumerationValueTranslations(nullable: false)
}
}
class EnumerationValueTranslation {
String value
Language language
static belongsTo = [ enumerationValue: EnumerationValue ]
static constraints = {
value(nullable: false, blank: false)
language(nullable: true, unique: 'enumerationValue')
enumerationValue(nullable: false)
/* unique constraint as mentioned in description text */
language(unique: 'enumerationValue')
}
}
到目前为止,这工作得很好。当我以语言交换的方式更新相同枚举值的两个枚举值翻译时,就会出现我的问题。例如我有一个
- 枚举值:“喜剧”
还有一些语言“不小心”混淆的翻译
- “喜剧”的翻译
- 语言:德语,值:“喜剧”
- 语言:英语,值“Komödie”
如果用户意识到他混淆了语言,他可能想要交换语言并再次保存枚举。这就是我的错误发生的地方,因为在交换语言后,枚举值翻译唯一约束验证为 false。
为了调试这个,我只是尝试在处理参数之前和之后打印出导致翻译的错误,所以:
Enumeration enumeration = Enumeration.get(params['id']);
println "before:"
enumeration.enumerationValues.each() { enumValue ->
enumValue.enumerationValueTranslations.each() { enumValueTr ->
println enumValueTr;
if(!enumValueTr.validate()) {
// print errors...
}
}
}
// swap languages:
// (this are the lines of codes that are actually executed, and cause the
// error. The actual processing of params looks different of course...)
// sets the language of "Comedy" to English
EnumerationValueTranslation.get(5).language = Language.get(1);
// sets the language of "Komödie" to German
EnumerationValueTranslation.get(6).language = Language.get(2);
println "after:"
enumeration.enumerationValues.each() { enumValue ->
enumValue.enumerationValueTranslations.each() { enumValueTr ->
println enumValueTr;
if(!enumValueTr.validate()) {
// print errors...
}
}
}
结果到:
before:
EnumerationValueTranslation(value: Fantasy, language: en_US, enumerationValue: Fantasy)
EnumerationValueTranslation(value: Phantasie, language: de_DE, enumerationValue: Fantasy)
EnumerationValueTranslation(value: Comedy, language: de_DE, enumerationValue: Comedy)
EnumerationValueTranslation(value: Komödie, language: en_US, enumerationValue: Comedy)
after:
EnumerationValueTranslation(value: Fantasy, language: en_US, enumerationValue: Fantasy)
EnumerationValueTranslation(value: Phantasie, language: de_DE, enumerationValue: Fantasy)
EnumerationValueTranslation(value: Comedy, language: en_US, enumerationValue: Comedy)
validation fails: Property [language] of class [Translation] with value [Language(code: en_US)] must be unique
EnumerationValueTranslation(value: Komödie, language: de_DE, enumerationValue: Comedy)
validation fails: Property [language] of class [Translation] with value [Language(code: de_DE)] must be unique
在这种状态下,我还没有删除或保存(或以任何方式刷新)任何内容 - 这只是更改对象后的结果。如您所见,实际数据中确实没有不一致,验证应该不会失败。
我更改翻译的方式可能有错误吗?我只是通过 ID 获取它们并简单地更新了语言 - 我在一个简约的例子中尝试了它,它在那里工作...... 如果我只是创建所有枚举值和枚举值翻译的深层副本并存储它(这意味着验证真的不应该失败),它也可以工作,但我认为这真的不是它应该的方式完成...
另一个奇怪的事情是,只有在我遍历数据时验证才会失败。如果我根本不接触数据,则不会发生错误,但也不会保存数据,这意味着以下几行会导致对验证进行评估:
enumeration.enumerationValues.each() { ev ->
ev.enumerationValueTranslations.each() { evt ->
}
}
这就是为什么我坚信一定存在一些不平凡的问题...如果您还有其他需要知道的,请告诉我。
感谢您的帮助
【问题讨论】:
-
这和stackoverflow.com/questions/6174374/…是同一个问题吗?
-
不,恐怕不是。我只是发现我对发生的实际错误是错误的。我真的很抱歉。我的旧请求可以因此被删除,因为它不能帮助我解决我的问题。但我真的很感激,如果你再给它一次机会,因为我认为这一次错误是非常严格的......