Grails 2.1如何防止级联删除

时间:2013-03-31 11:07:07

标签: grails-2.0

人们使用哪些技术来有效地防止对大量类的级联删除?

我有以下域类:但是当我删除一个类别项时,它也会删除与类别相关的所有genericProduct项。现在我该怎样阻止这个

class Category {

String name
static hasMany      =  [manufacturers: Manufacturer, genericProducts:GenericProduct]
static mappedBy     =  [genericProducts:'categories']
AuditUser audit     =  new AuditUser()
static embedded     =  ['audit']    
static constraints  =  {
    name  blank: false, size: 0..60, unique: false
}
static mapping = {
    table 't01i001'
    id      column: 'f_category_id'
    name    column: 'f_name',       length: 60     
    version column: 'f_revision'

    manufacturers joinTable: [name: 't01j001', key: 'k_category_id', column: 'k_manufacturer_id']
}

class GenericProduct {

String name

static belongsTo  =  [categories:Category]
static hasMany    =  [brands:Brand]
static mappedBy   =  [brands:'genericProducts']

AuditUser audit = new AuditUser()
static embedded = ['audit']  

static constraints = {
    name     blank: false, size: 0..60, unique: false
}

static mapping = {
    table 't01i003'
    id          column: 'f_generic_product_id'
    name        column: 'f_name', length: 30  
    categories  column: 'f_cat_id', length: 30  
    version     column: 'f_revision'
}

1 个答案:

答案 0 :(得分:2)

下次我添加一些配置以防止级联删除然后它可以正常工作

class Category {

    String name
    static hasMany      =  [manufacturers: Manufacturer, genericProducts:GenericProduct]
    static mappedBy     =  [genericProducts:'categories']
    AuditUser audit     =  new AuditUser()
    static embedded     =  ['audit']    
    static constraints  =  {
        name  blank: false, size: 0..60, unique: false
    }
    static mapping = {
        manufacturers   cascade: 'save-update'
        genericProducts cascade: 'save-update'

        table 't01i001'
        id      column: 'f_category_id'
        name    column: 'f_name',       length: 60     
        version column: 'f_revision'

        manufacturers joinTable: [name: 't01j001', key: 'k_category_id', column: 'k_manufacturer_id']
    }