gpt4 book ai didi

grails - GORM - all-delete-orphan 不起作用

转载 作者:行者123 更新时间:2023-12-02 14:12:47 27 4
gpt4 key购买 nike

我在 mygrails 项目中有 2 个类,称为 Profile 和 License
以下是类(class)中重要内容的大纲

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
}
}

当我在我的一个 Controller 中运行以下代码时
    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 个错误
key 已从 Profile 类中设置的 licenceKeys 中删除,但仍保留在数据库中

我有 2 个问题
是否有可能为我收到的错误消息获得更好的调试输出
我是否遗漏了任何东西来确保从数据库中删除 licekeys?

谢谢

最佳答案

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; }
}

关于grails - GORM - all-delete-orphan 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13395056/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com