如何删除grails中的子域行

时间:2015-02-11 10:29:23

标签: oracle grails gorm grails-2.0 grails-domain-class

我将数据保存到我的入口会议表以及入口会议详细信息表,这是一个入口会议表的子表。但是,当我只想删除子表记录时,它会给出错误。我无法理解该怎么做。我正在使用grails 2.1.0。请问有人可以帮我吗?!!!以下是我的代码:

我的父域>>>

class AdtEntryMeeting {
    static mapping = {
        table('ADT_ENTRY_MEETING')
        version(false)
        entryMeetingDetails cascade: 'all'
    }
    String meetingVenue
    Date meetingDate = new Date()
    String meetingTime
    long CREATED_BY=0
    Date CREATED_DATE=new Date()
    long UPDATED_BY=0
    Date UPDATED_DATE=new Date()
    static hasMany = [entryMeetingDetails: AdtEntryMeetingDetails]
    static constraints = {
        meetingVenue(nullable: false, blank: false)
        meetingDate(nullable: false)
        meetingTime(nullable: false, blank: false)
        CREATED_BY(nullable:true)
        CREATED_DATE(nullable:true)
        UPDATED_BY(nullable: true)
        UPDATED_DATE(nullable: true)
    }
    String toString(){
        return id
    }
}

我的子域>>>

class AdtEntryMeetingDetails {
    static mapping = {
        table('ADT_ENTRY_MEETING_DETAILS')
        version(false)
    }
    AuditFirm auditPack
    AuditorDtl auditor
    String meetingSubject
    String responsiblePerson
    AdtEntryMeeting entryMeeting
    static constraints = {
        auditPack(nullable: false, blank: false)
        auditor(nullable: false, blank: false)
        meetingSubject(nullable: false, blank: false)
        responsiblePerson(nullable: false, blank: false)
        entryMeeting(nullable: false, blank: false)
    }
    String toString(){
        return auditPack
    }
}

我要删除的操作>>>

if(params[isDelete + detailsCounter] == 'delete'){
                def entryMeetingDetailsDelete = AdtEntryMeetingDetails.get(Long.parseLong(params[entryMeetingDetailsId + detailsCounter]))
                entryMeetingDetailsDelete.delete()
            }else{
                def entryMeetingDetailsEdit = AdtEntryMeetingDetails.get(Long.parseLong(params[entryMeetingDetailsId + detailsCounter]))
                entryMeetingDetailsEdit.properties['auditPack'] = AuditFirm.get(Long.parseLong(params[auditPack + detailsCounter]))
                entryMeetingDetailsEdit.properties['auditor'] = AuditorDtl.get(Long.parseLong(params[auditor + detailsCounter]))
                entryMeetingDetailsEdit.properties['meetingSubject'] = params[meetingSubject + detailsCounter]
                entryMeetingDetailsEdit.properties['responsiblePerson'] = params[responsiblePerson + detailsCounter]
                adtEntryMeetingInstance.addToEntryMeetingDetails(entryMeetingDetailsEdit)
            }

我得到的错误>>>

deleted object would be re-saved by cascade (remove deleted object from associations)

1 个答案:

答案 0 :(得分:1)

您应首先从父级删除引用,然后删除子级

例如

def entryMeetingDetailsDelete = AdtEntryMeetingDetails.get(Long.parseLong(params[entryMeetingDetailsId + detailsCounter]))

adtEntryMeetingInstance.removeFromAdtEntryMeetingDetails(entryMeetingDetailsDelete)
 entryMeetingDetailsDelete.delete()