验证域类的实例是唯一的 [英] Validate instance of domain class is unique

查看:92
本文介绍了验证域类的实例是唯一的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆Grails应用程序,它们有许多领域类,其中一些领域有一些与所讨论的领域类有 hasMany 关系。对于这个特定的应用程序,我只有一个约束,那就是每个域类的每个实例都必须是唯一的。我不关心一个任意字段在同一个域类的多个实例中是否具有相同的值,只要每个实例由该域类中某个其他字段的值唯一确定即可。所以基本上我想验证发生在域类实例级别而不是域级别字段级别。现在我正在通过使用非常常用的 @EqualsAndHashCode 注释来生成我的等于 hashCode 方法,然后在域类的某个任意字段的自定义验证器中调用 equals

I have a Grails application with a bunch of domain classes, some with many fields, some of which have a hasMany relationship with the domain class in question. For this particular application I only have one "constraint", that is that every instance of every domain class must be unique. I don't care if an arbitrary field has the same value across multiple instances of the same domain class, so long as each instance is made unique by the value of some other field in that domain class. So basically I want validation to take place at a domain class instance level instead of the domain class field level. Right now I am doing that by using the very groovy @EqualsAndHashCode annotation to generate my equals and hashCode methods, then calling equals in a custom validator on some arbitrary field of a domain class.

有两个问题:

Two questions:


  1. 是否有更有效的验证域类的方法是唯一的?

  2. 如果否,那么是否有办法在域类实例本身上调用自定义验证程序代码,而不是通过域类实例的某个字段?
  3. >
  1. Is there a more efficient way of validating a domain class is unique?
  2. If no, then is there a way I can call my custom validator code on the domain class instance itself instead of going through one of the fields of the domain class instance?




@groovy.transform.EqualsAndHashCode
class MyDomainClass {
    String some
    int arbitrary
    boolean field
    static constraints = {
        // The field I chose to validate on is irrelivant, I just need to run the validation code **somewhere**
        arbitrary validator: { val, obj ->
            return !MyDomainClass.getAll().contains(obj)
         }
    }
}

我也应该添加我正在寻找一个通用的(希望有效的)方式来做到这一点。我知道调用 getAll()非常低效,而是调用一些查找的变体或者对确切地执行HQL查询每个领域类的字段将会更有效率......写作需要更长的时间!

I should also add I'm looking for a generic (hopefully efficient) way to do this. I realize calling getAll() is very inefficient and instead calling some variant of find or performing an HQL query on the exact fields of each domain class would be much more efficient... it just takes a lot longer to write!

示例

    assert MyDomainClass.getAll().isEmpty() // true

    def myDomainClass1 = new MyDomainClass( some: "foo", arbitrary: 1, field: true)
    assert MyDomainClass.getAll().contains(myDomainClass1); // false
    myDomainClass1.save(flush:true)

    def myDomainClass2 = new MyDomainClass( some: "bar", arbitrary: 1, field: true)
    assert MyDomainClass.getAll().contains(myDomainClass2); // false.  Even though this has the same `arbitrary` value as myDomianClass1, it has a different `some` value which makes it unique.
    myDomainClass2.save(flush:true)

    def myDomainClass3 = new MyDomainClass( some: "foo", arbitrary: 1, field: false)
    assert MyDomainClass.getAll().contains(myDomainClass3); // false.  Even though this has the same `some` value as myDomainClass1 and the same `arbitrary` value as myDomainClass1 and myDomainClass2, it has a different `field` value which makes it unique.
    myDomainClass3.save(flush:true)


推荐答案

这将确保域中3个字段的组合是唯一的。这也确保约束在数据库级别,而不仅仅是应用程序级别。

This will ensure the combination of the 3 fields in the domain are unique. This also ensures the constraint is on the database level, instead of just application level.

class MyDomainClass {
    String some
    int arbitrary
    boolean field
    static constraints = {
        some(unique: ['arbitrary', 'field'])
    }
}

这篇关于验证域类的实例是唯一的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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