在 grails 中使用自定义 id 时出现错误

I have an error when using custom id in grails

我想要一个自定义 ID,因为默认情况下 grails 会处理 ID 名称。我有一个这样的域 class:

class User {
    Long id_user
    String name
...
    static mapping = {
        id generator: 'increment', name: "id_user", type: 'Long'
    }
...    
}

当我保存用户时,在保存操作中出现以下错误“Can not redirect for object [project.User: (unsaved)] it is not a domain or has no identifier. Use an explicit redirect instead

def save(User user) {
    if (user == null) {
        notFound()
        return
    }

    try {
        userService.save(user)
    } catch (ValidationException e) {
        respond usuario.errors, view:'create'
        return
    }

    request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.created.message', args: [message(code: 'user.label', default: 'User'), user.id])
            redirect user
        }
        '*' { respond user, [status: CREATED] }
    }
}

为了更正错误,我尝试将 user.id 用户放入保存功能,但它也不起作用。

在没有构建测试用例来重现它的情况下,它看起来很像

redirect user

正在尝试重定向到 /user/show/<user.id>,如错误消息所示,请考虑将其替换为

redirect(action: 'show', params:[id:user.id_user])

我不精通自定义 id,所以我不确定所有细微差别,但您使用的重定向快捷方式可能无法正确遵守它们。