mongodb - grails,mongodb gorm,null嵌入式对象

标签 mongodb grails gorm gorm-mongodb

我有两个简单的域类:

class Isa95EquipmentSpecification {

    String equipmentClass
    String equipment
    String description
    Float quantity
    String quantityUOM
    List<Isa95EquipmentSpecificationProperty> equipmentSpecificationProperties

    static embedded = ['equipmentSpecificationProperties']

    static constraints = {
        equipment nullable: true, validator: {val, obj -> if (null == val && null ==    obj.equipmentClass) return ['bothNullable']}
        equipmentClass nullable: true, validator: {val, obj -> if (null == val && null == obj.equipment) return ['bothNullable']}
        description nullable: true
        quantity nullable: true
        quantityUOM nullable: true  
    }
}

和子域:
class Isa95EquipmentSpecificationProperty {
    String name
    String description
    String value
    String valueUOM
    Double quantity
    String quantityUOM

    static constraints = {
        name nullable: false
        description nullable: true
        value nullable: false
        valueUOM nullable: false
        quantity nullable: true
        quantityUOM nullable: true
    }
}

我希望建立一个包含嵌入式属性的组合文档,我可以保存它,只在父级上运行一次save()操作,但它不起作用。

我尝试在grails控制台上运行:
    def prop1 = new isa95.productdefinition.Isa95EquipmentSpecificationProperty(name: 'prop-1', value: 'mad', valueUOM: '-')
def prop2 = new isa95.productdefinition.Isa95EquipmentSpecificationProperty(name: 'prop-2', value: 12.32, valueUOM: 'kilograms')
def spec = new isa95.productdefinition.Isa95EquipmentSpecification(equipment: '41500', description: 'eq-test', equipmentSpecificationProperties: [prop1, prop2])
spec.save(failOnError: true)

该脚本可以正确运行,但是在数据库中我发现了这一点。我希望找到用嵌套列表填充的equipmentSpecificationProperies:
{ "_id" : NumberLong(9), "description" : "eq-test", "equipment" : "14500", "equipmentSpecificationProperties" : [ null, null ], "version" : 0 }

最佳答案

指定列表类型:

List<Isa95EquipmentSpecificationProperty> equipmentSpecificationProperties

关于mongodb - grails,mongodb gorm,null嵌入式对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27561500/

相关文章:

javascript - meteor.js - 设置 Controller 来访问集合

java - MongoDB 3.3.0以上版本中QueryBuilder和BasicDBObjectBuilder的使用

rest - 连续轮询作为后台服务

Grails 服务类交叉引用

grails - 固定时间间隔后如何使用grails重新索引elasticsearch数据?

grails - Grails/GORM:方法签名不适用于参数类型

grails - 为什么此Grails(GORM)查询无效?

mongodb - 锁定在我自己的 mongodb 实例之外

sql - NOSQL 数据库中有存储过程之类的东西吗?

grails - PagedResultList .size()和.getTotalCount()在grails gorm中返回不同的值