0

(Grails 2.2.4)

When I execute this:

def AuditLog auditLog=new AuditLog(appUserId: 1488902, auditDate: new Date(), action: "Update Questionnaire", username: "USER1" )
auditLog.addToAuditTargets(new AuditTarget(targetId: 100, type: "what's the type"))
auditLog.save(failOnError:true)

I get this error:

| Error 2014-01-04 00:39:38,904 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application: Validation Error(s) occurred during save(): - Field error in object 'com.company.kyn.model.AuditLog' on field 'auditTargets[0].auditId': rejected value [null]; codes [com.company.kyn.model.AuditTarget.auditId.nullable.error.com.company.kyn.model.AuditLog.auditTargets[0].auditId...]; arguments [auditId,class com.company.kyn.model.AuditTarget]; default message [Property [{0}] of class [{1}] cannot be null]

The generated auditId is not set on AuditTarget. How do I fix this?

Thank you.

package com.company.kyn.model

class AuditLog {

Date auditDate
String action
String username
Long appUserId

static hasMany = [auditTargets: AuditTarget]

static mapping = {
    id column: "AUDIT_ID", generator: "hilo"
    auditTargets joinTable:false
    version false
}

static constraints = {
    action maxSize: 100
    username maxSize: 100
}
}

package com.company.kyn.model

import org.apache.commons.lang.builder.EqualsBuilder
import org.apache.commons.lang.builder.HashCodeBuilder

class AuditTarget implements Serializable {

Long auditId
String type
Long targetId

static belongsTo = [auditLog:AuditLog]

static mapping = {
    id composite: ["auditId", "type", "targetId"]
    auditLog column: "AUDIT_ID"
    version false
}

static constraints = {
    type maxSize: 100
}

int hashCode() {
    def builder = new HashCodeBuilder()
    builder.append auditId
    builder.append type
    builder.append targetId
    builder.toHashCode()
}

boolean equals(other) {
    if (other == null) return false
    def builder = new EqualsBuilder()
    builder.append auditId, other.auditId
    builder.append type, other.type
    builder.append targetId, other.targetId
    builder.isEquals()
}
}
umk
  • 43
  • 6

1 Answers1

0

Update: (Solution)

First of all you need to remove

Long auditId

from AuditTarget, because apparently you are maintaining two mappings.

Then rewrite you AuditTarge domain as:

class AuditTarget implements Serializable{

    String type
    Long targetId

    static belongsTo = [auditLog:AuditLog]

    static mapping = {
        id composite: ["auditLog","type", "targetId"]
        auditLog column: "AUDIT_ID"
        version false
    }

    static constraints = {
        type maxSize: 100
    }

    int hashCode() {
        def builder = new HashCodeBuilder()
        builder.append auditLog
        builder.append type
        builder.append targetId
        builder.toHashCode()
    }

    boolean equals(other) {
        if (other == null) return false
        def builder = new EqualsBuilder()
        builder.append auditLog, other.auditLog
        builder.append type, other.type
        builder.append targetId, other.targetId
        builder.isEquals()
    }
}

This will solve your issue

dhamibirendra
  • 3,016
  • 23
  • 26
  • Thanks but that didn't solve it. When I query the AuditTarget table there's no data in it. I changed save() in your code to save(failOnError:true) and got the same error. The AuditLog.auditTargets[0].auditId is null so the generated key from AuditLog is not being propagated to the AuditTarget instance. I've looked over the GORM docs but cannot find a solution. – umk Jan 04 '14 at 17:34
  • Your suggestions worked! Thank you. I'm not sure how you figured out to use auditLog in the composite key... did I miss that in the GORM docs? – umk Jan 05 '14 at 01:57
  • hmm.. the secret is read, code and learn :) and Especially for Grails follow blogs.. like http://mrhaki.blogspot.com/ – dhamibirendra Jan 05 '14 at 02:00
  • also check this: http://grails.org/doc/2.3.4/guide/GORM.html#compositePrimaryKeys – dhamibirendra Jan 05 '14 at 02:05