Grails无法使用级联清除hasMany集

时间:2016-08-12 18:24:12

标签: grails gorm cascading-deletes

我正在尝试清除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

控制器:

AppSystemUser user = AppSystemUser.findBySystemUserid(cmd.netid);
user.userDeptses.clear();
userMgmtService.saveAppSystemUser(user);

AppSystemUser:

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:

class UserDepts {

Boolean active
AppSystemUser appSystemUser
Department department

static belongsTo = [AppSystemUser, Department]

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

....

UserMgmtService:

@Transactional
class UserMgmtService {
    def saveAppSystemUser(AppSystemUser user) {
        user.save();
    }
}

1 个答案:

答案 0 :(得分:0)

我无法找到如何成功使用Set的clear()方法,所以我只是创建了一个简单的解决方法来实现这个技巧

def static hibernateSetClear(Set data) {
    if(data) {
        Iterator i = data.iterator();
        while (i.hasNext() && i.next()) {
            i.remove();
        }
    }
}

只需遍历Set并逐个删除每个项目。这非常有效,只要我需要清除clear()

,我就会调用此方法而不是Set