(Grails 2.2.4)
执行此操作时:
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)
我收到此错误:
|错误2014-01-04 00:39:38,904 [localhost-startStop-1]错误context.GrailsContextLoader - 初始化应用程序时出错:验证在save()期间发生错误: - 对象'com.company.kyn.model.AuditLog'中字段'auditTargets [0] .auditId'中的字段错误:被拒绝的值[null] ;代码[com.company.kyn.model.AuditTarget.auditId.nullable.error.com.company.kyn.model.AuditLog.auditTargets [0] .auditId ...]; arguments [auditId,class com.company.kyn.model.AuditTarget];默认消息[类[{1}]的属性[{0}]不能为空]
未在AuditTarget上设置生成的auditId。我该如何解决这个问题?
谢谢。
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()
}
}
答案 0 :(得分:0)
更新:(解决方案)
首先,你需要删除
Long auditId
来自AuditTarget的,因为显然你正在维护两个映射。
然后将AuditTarge域重写为:
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()
}
}
这将解决您的问题