Grails addTo重复项

Grails addTo duplicates

我建立了以下关联:

class Applicant {
    String appStepsComplete
    String name
    String eNumber
    String email
    String homePhone
    String cellPhone
    String address
    Integer age

    static hasMany = [scholarships:Scholarship]

    static mapping = {
        scholarships joinTable: [name:"APPLICANT_SCHOLARSHIPS"]
    }
}

class Scholarship {
    String fundCode
    String seqNo
    String name

    static belongsTo = Applicant
}

当我调用它时,它允许将重复项添加到数据库中:

 applicant.scholarships << schol
 applicant.save()

我需要它来防止数据库中出现重复项。我尝试通过执行以下操作对申请人的奖学金设置唯一限制,但它没有用:

static constraints = {
    scholarships(unique:true)
}

Burt Beckwith 的评论是正确的,您需要覆盖奖学金上的 hashCode 和 equals。假设业务键(唯一标识此实体的字段组合,如果数据库未使用人工 ID,您将用作复合自然键)是 fundCode 和 seqNo 的组合,您可以使用类似:

int hashCode() {
    (fundCode + seqNo).hashCode()
}

boolean equals(Object other) {
    other?.getClass() == this.class
    && other.fundCode == fundCode 
    && other.seqNo == seqNo
}

hashCode 实现可能不是有史以来性能最好的东西,它是一种懒惰的方式,依靠 String 的 hashCode。但这足以说明它是否解决了欺骗问题。

DRYer 解决方案是使用带有此注释的 AST 转换

import groovy.transform.EqualsAndHashCode

@EqualsAndHashCode(includes=['fundCode', 'seqNo'])
class Scholarship {
    String fundCode
    String seqNo
    String name

    static belongsTo = Applicant
}

这将为您生成 equals 和 hashCode 方法。

Set 实现依赖于这些方法来决定两个对象实例是否表示相同的信息。不覆盖意味着唯一的检查是引用是否相同(因此,如果您有具有相同信息的不同对象实例,它们将被视为两个不同的对象)。使用业务信息而不是 id 来检查相等性意味着无论域对象是否分配了 id,它都会起作用。