Grails 2.3.11 - 对象无法持久化

Grails 2.3.11 - Object failing to persist

我正在尝试使用 Grails 2.3.11 修改服务中的某些实体。我已精简为以下代码:

void updateCompanyType(CompanyType type, Map properties) {
    def role = properties.role?.id ==~ /\d+/ ? Role.get(properties.role.id) : null

    role.name = "Super New Name"
    role.save(failOnError: true, flush: true)

    if (type.role != role) {
        type.role = role
    }

    type.name = properties.name
    type.save(failOnError: true)
}

类型成功更新,但角色没有。任何洞察为什么?我知道在服务调用中,我处于事务中,所以我在有和没有显式保存调用的情况下都尝试过,这似乎没有什么区别。

部分看角色class:

class Role implements Comparable {

  Integer id
  String name
  String code
  RoleType roleType

尝试改变

def role = properties.role?.id ==~ /\d+/ ? Role.get(properties.role.id) : null

进入

def role = properties.role?.id ==~ /\d+/ ? Role.get(properties.role.id) : new Role()

如果您正在使用 Spring 安全角色对象,您是否扩展它以将 "name" 添加到该对象?如果是这样,您是否在扩展对象中将其定义为 "def name" 或 "String name"?

无论哪种方式,如果您在域 class 中将 属性 声明为 "def" 或 "private" 它不会将其持久保存到数据库中,即使您在调试器中查看它,它将具有 属性 和值。此外,如果您使用的是内置数据库模式生成器实用程序和迁移,它实际上是在 "role" table 中创建了一个 "name" 列,还是您手动创建的?

需要查看您的域 class 在 "Role" 中的样子吗?

我终于通过设置

让它工作了

version true

在映射部分中我的角色域对象上。