grails,mongodb gorm,null embedded objects

时间:2014-12-19 07:28:38

标签: 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)

脚本运行正常但在db中我找到了这个。我期望找到使用嵌套列表填充的equipmentSpecificationProperies:

{ "_id" : NumberLong(9), "description" : "eq-test", "equipment" : "14500", "equipmentSpecificationProperties" : [ null, null ], "version" : 0 }

1 个答案:

答案 0 :(得分:0)

指定列表类型:

List<Isa95EquipmentSpecificationProperty> equipmentSpecificationProperties