交换属性值后,Grails唯一测试失败 [英] Grails unique test fails after interchanging attribute values

查看:131
本文介绍了交换属性值后,Grails唯一测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试实现一个简单的翻译列表,这意味着我有一个值和翻译到这个值。



因为这是我的一部分用户界面和值和翻译应该可以通过xml导出,使用i18n文件接口对于这个命题非常不方便。这就是为什么我决定将它们存储在数据库中。



我有一个值为域的类:

  class Value {
字符串标签
static hasMany = [translations:Translation]
}

,另一个用于具有唯一约束的翻译,以确保对于一个值,对于特定语言,必须有多个翻译:

  class翻译{
字符串值
语言语言

static belongsTo = [value:Value]

static constraints = {
language(unique:'value')
}
}

在为相同的值交换两种翻译语言后,我的问题发生了。
示例:

  value.translations.each(){translation  - > 
println $ {translation.language.label}中的$ {value.label}是$ {translation.value}
}

//进程更新...

value.translations.each(){translation - >
println $ {translation.language.label}中的$ {value.label}是$ {translation.value}
}

//验证...

打印出来

 喜剧德语:喜剧
喜剧英文:科莫迪

喜剧英文:喜剧
德语喜剧:科姆迪

所以在更新之前和之后不会违反唯一约束,但无论如何,我在保存时会得到唯一的约束失败。
另一个奇怪的事情是,当我在value上执行each()循环时,我只会得到这个错误。如果我不检查内容,验证通过并且save(flush:true)方法返回true,但值不会在数据库中更改。



我认为问题出现在数据库级别,只有一个值被修改,另一个不是,因为恰好在该状态下约束被违反。如果更改将作为交易执行,而在此中间步骤中不会检查约束,则可以避免这种情况。 (这可能是我在找的东西)



避免这种情况的另一种方法是删除并重新创建每个已编辑的bean,但我希望可能有一个

感谢您的帮助

解决方案

在隐式或显式 flush()发生时检查约束。此时,GORM会检查数据库中是否存在另一个。因此,如果一个实例已经是 flush() ed,而另一个实例尚未实现,那么您将违反约束条件。



尽量不要 flush()直到事务结束 - 删除 flush:true 参数,甚至设置它到 flush:false 。在交易结束时,这两个更改都应该适用。



Grails中存在一个警告,JFYTK:它含有 flush()在执行 Criteria 时,所以当你不打算 flush()还没有。


Hello I am trying to implement a simple translation list, meaning I have a values and translations to this values.

[Edit:] As this is part of my user interface and values and translations shall be exportable via xml, using the i18n files seams quite inconvinient for this proposition. Thats why I decided to store them in a database.

I have one domain class for a value:

class Value {
    String label
    static hasMany = [ translations: Translation ]
}

and one for translations with a unique constraint to ensure that for one value there must'nt be more than one translation for a specific language:

class Translation {
    String value
    Language language

    static belongsTo = [ value: Value ]

    static constraints = {
        language(unique: 'value')
    }
}

My problem occures after interchanging two translation languages for the same value. Example:

value.translations.each() { translation ->
    println "${value.label} in ${translation.language.label} is ${translation.value}"
}

// process updates...

value.translations.each() { translation ->
    println "${value.label} in ${translation.language.label} is ${translation.value}"
}

// validate...

prints out

Comedy in german: Comedy
Comedy in english: Komödie   

Comedy in english: Comedy
Comedy in german: Komödie

so the unique constraint is not violated before and after the update, but anyhow I get an unique constraint failure while saving. Another strange thing is, that I only get this error when I execute the each() loop on value. If I don't inspect the contents, the validation passes and the save(flush:true) method returns true, but the values will not be changed in the database.

[Edit:] I believe that the problem is on database level when only one value is altered and the other isn't, because exactly in that state the constraint is violated. If the changes would be executed as a transaction instead and the constraints would not be checked during this intermediate step this could be avoided. (this might be the thing, i am looking for)

Another way to avoid this would be to delete and recreate every edited bean but I was hoping that there might be a more convenient way to do this.

Thanks for any help

解决方案

The constraint is checked when an implicit or explicit flush() happens. At that moment, GORM checks if there exists another such a value in a database. So, if one instance is already flush()ed and another is not yet, you'll get a constraint violation.

Try not to flush() till the end of transaction - remove flush: true parameter or even set it to flush: false. At the end of transaction both changes should apply.

There's a caveat in Grails, JFYTK: it does an implicit flush() when executing a Criteria, so don't be too surprised about Hibernate errors when you didn't intend to flush() yet.

这篇关于交换属性值后,Grails唯一测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆