Search code examples
grailsgrails-orm

gorm - cannot save object to database


I have an Audit table with primary key as a composite key of columns REQUEST_ID, AUDIT_TIMESTAMP.

I wrote my domain class as follows

@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'

    }}


and my test class follows :

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)
        }

   }
}

Not sure why my object is not persisted to the database. I dont see any errors or exceptions on the log. Tests passed successfully. Not sure what is wrong.


Solution

  • after using failOnError: true, I figured that the issue is not about the composite key. Some other column was causing the issue. Fixed. And I dint have to use @EqualsAndHashCode and dint have to implement equals or hashcode methods.