gorm - 无法将对象保存到数据库

gorm - cannot save object to database

我有一个审计 table,其中主键作为列 REQUEST_ID, AUDIT_TIMESTAMP 的复合键。

我把我的域名class写成如下

@EqualsAndHashCode
class Audit implements Serializable {

    Integer     requestId
    Timestamp   timestamp

    boolean equals(other) {
        if (!(other instanceof Audit)) {
            return false
        }

        other.requestId == requestId && other.timestamp == timestamp
    }

    int hashCode() {
        def builder = new HashCodeBuilder()
        builder.append requestId
        builder.append timestamp
        builder.toHashCode()
    }

    static constraints = {
        requestId       size: 19
    }

    static mapping = {

        table           'AUDIT'
        version         false
        id              composite: ['requestId', 'timestamp']
        requestId       column: 'REQUEST_ID', index: 'AUDIT_IDX'
        timestamp       column: 'AUDIT_TIMESTAMP'

    }}


我的测试 class 如下:

class AuditIntegrationSpec extends Specification {

    Audit auditLog;

    def setup() {
        auditLog = new Audit()
        auditLog.setRequestId(12)
        auditLog.setTimestamp(new Timestamp(new Date().getTime()))

    }

    void "test something"() {
        when : {
            auditLog.save(flush: true)
        }

   }
}

不确定为什么我的对象没有保存到数据库中。我在日志中没有看到任何错误或异常。测试成功通过。不知道哪里出了问题。

使用failOnError: true后,我发现问题与组合键无关。其他一些专栏导致了这个问题。固定的。我必须使用 @EqualsAndHashCode 并且必须实现 equalshashcode 方法。