我试图从userDeptses集中清除数据,但是当我调用clear()方法并试图保存时,我得到以下错误
Caused by: java.lang.UnsupportedOperationException: queued clear cannot be used with orphan delete
我有正确的级联设置,我甚至尝试使用delete-orphan,但仍然有问题。所有相关类都实现了equals和hashCode方法: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();
}
}发布于 2016-08-15 19:31:18
我无法找到如何成功地使用集合的clear()方法,所以我只是创建了一个简单的解决方法来完成这个任务。
def static hibernateSetClear(Set data) {
if(data) {
Iterator i = data.iterator();
while (i.hasNext() && i.next()) {
i.remove();
}
}
}只需遍历Set并单独删除每个项即可。这很好,每当我需要清除一个clear()时,我就调用这个方法而不是Set。
https://stackoverflow.com/questions/38924295
复制相似问题