2012-11-15 67 views
2

我有两个班在mygrails项目名为Profile和许可 这里的东西,在类重要的轮廓GORM - 所有 - 删除 - 孤儿不工作

class Profile { 

    Set<Licence> licenceKeys 

    static hasMany = [licenceKeys:Licence] 

    static constraints = { 
     licenceKeys nullable:true 
    } 
    static mapping = { 
     licenceKeys cascade: 'all-delete-orphan' 
    } 
} 

class Licence { 

    Profile profile 
    String licenceKey 

    static belongsTo = [profile:Profile] 

    static constraints = { 
     profile nullable:false 
     licenceKey blank:true, nullable:true, unique:true 
    } 
} 

当我运行下面的代码我的控制器之一

if (!profile.licenceKeys.clear()) { 
     log.error ("Error occured removing licence keys from the database"); 
     userProfile.errors.each { err -> println err; } 
    } else { 
     log.info("Successfully remove licence keys from the database"); 
    } 

我在控制台收到以下错误消息,并且licencekeys保留在数据库 org.springframework.validation.BeanPropertyBindingResult:0错误 关键s是在Profile类确定,但licenceKeys取出留在DATABSE

我有2个问题 是有可能得到的,我得到了 我错过了什么,以确保licekeys错误消息更好的调试输出从数据库中删除?

感谢

回答

0

clear()方法实际上并不会做删除形成你想要的clear()后保存数据库的工作。试试...

profile.licenceKeys.clear(); 
if (!profile.save(flash:true)) { 
    log.error ("Error occured removing licence keys from the database"); 
    userProfile.errors.each { err -> println err; } 
} else { 
    log.info("Successfully remove licence keys from the database"); 
} 

关于第二个问题,你会想从失败节约像这样的域对象的错误....

if (!profile.save(flash:true)) { 
    log.error ("Error occured removing licence keys from the database"); 
    profile.errors.each { err -> println err; } 
}