Pregunta

(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()
}
}
¿Fue útil?

Solución

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top