无法使用gorm(typeMismatch错误)在mongodb中保存嵌入对象

时间:2017-03-31 21:33:12

标签: grails gorm-mongodb

我正在尝试使用gorm和mongodb插件创建和保存域对象,但我总是在嵌入属性上遇到类型不匹配错误(“emailTemplates”)。

我的域名定义为:

import org.apache.commons.mail.Email
import org.bson.types.ObjectId

class EmailTemplate {

    ObjectId id
    String subject
    String message
    String locale
    String from
    List<String> to
    List<String> cc
    List<String> bcc

    static mapWith = "mongo"

    static constraints = {
        subject(blank: false, maxSize: 78)
        message(blank: false)
        locale(blank: false)
    }

    static mapping = {
        locale defaultValue: "en_US"
    }
}

class Template {

    ObjectId id
    String namespace
    String name
    String description
    String defaultLocale
    Boolean enabled
    List emailTemplates

    static mapWith = "mongo"
    static hasMany = [emailTemplates: EmailTemplate]
    static embedded = ["emailTemplates"]
    static constraints = {
        name(unique: ['namespace'], blank: false, maxSize: 250)
        description(blank: true)
        enabled(blank: false)
        namespace(blank: false)
        defaultLocale(blank: false)
    }
    static mapping = {
        id attr: "_id"
        defaultLocale defaultValue: "en_US"
        enabled defaultValue: true
    }

}

我的POST有效负载json是:

{
  "namespace": "some namespave",
  "emailTemplates": [
    {
      "locale": "en_US",
      "from": "xyz@gmail.com",
      "message": "test",
      "subject": "test"
    }
  ],
  "name": "a name",
  "defaultLocale": "en_US",
  "enabled": true,
  "description": "test"
}

现在我有一个解决方法,我在控制器上覆盖“save”方法(扩展RestfulController)并手动实例化域对象,但我宁愿让默认的“RestfulController”工作。

我使用的是mongodb插件版本6.1.0,mongodb-driver:3.4.2,我的grails版本是3.2.8

0 个答案:

没有答案