首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Grails不能使用级联清除hasMany集。

Grails不能使用级联清除hasMany集。
EN

Stack Overflow用户
提问于 2016-08-12 18:24:12
回答 1查看 321关注 0票数 0

我试图从userDeptses集中清除数据,但是当我调用clear()方法并试图保存时,我得到以下错误

Caused by: java.lang.UnsupportedOperationException: queued clear cannot be used with orphan delete

我有正确的级联设置,我甚至尝试使用delete-orphan,但仍然有问题。所有相关类都实现了equalshashCode方法:AppSystemUser UserDepts Department

我在网上读过的所有文档和文章都说,使用clear()all-delete-orphan的结合应该是可行的,但对我来说就不行了。任何帮助都是非常感谢的。

Grails 3.1.4

控制器:

代码语言:javascript
复制
AppSystemUser user = AppSystemUser.findBySystemUserid(cmd.netid);
user.userDeptses.clear();
userMgmtService.saveAppSystemUser(user);

AppSystemUser:

代码语言:javascript
复制
class AppSystemUser {

String systemUserid
String email
String fullName
Date lastLogin
Boolean active
Set appSystemUserRoles = [];
Set userCollegeses = [];
Set userDeptses = [];

static hasMany = [appSystemUserRoles: AppSystemUserRole,
                  applicationExtensions: ApplicationExtension,
                  userCollegeses: UserColleges,
                  userDeptses: UserDepts]

static mapping = {
    version false
    fullName column: 'fullName'
    lastLogin column: 'lastLogin'
    id name: "systemUserid", generator: "assigned"
    appSystemUserRoles cascade: "save-update, all-delete-orphan"
    userCollegeses cascade: "save-update, all-delete-orphan"
    userDeptses cascade: "save-update, all-delete-orphan"
}

....

UserDept:

代码语言:javascript
复制
class UserDepts {

Boolean active
AppSystemUser appSystemUser
Department department

static belongsTo = [AppSystemUser, Department]

static mapping = {
    version false
    appSystemUser column: "system_userid"
}

....

UserMgmtService:

代码语言:javascript
复制
@Transactional
class UserMgmtService {
    def saveAppSystemUser(AppSystemUser user) {
        user.save();
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-08-15 19:31:18

我无法找到如何成功地使用集合的clear()方法,所以我只是创建了一个简单的解决方法来完成这个任务。

代码语言:javascript
复制
def static hibernateSetClear(Set data) {
    if(data) {
        Iterator i = data.iterator();
        while (i.hasNext() && i.next()) {
            i.remove();
        }
    }
}

只需遍历Set并单独删除每个项即可。这很好,每当我需要清除一个clear()时,我就调用这个方法而不是Set

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38924295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档