0
votes

I have this two simple domain classes:

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  
    }
}

and the child domain:

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
    }
}

I expect that building up a composed document whit embedded properties I can save it running just a save() operation on the parent but it doesn't work.

I try to run on the grails console:

    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)

The script run correctly but in the db I found this. I expected to find equipmentSpecificationProperies populated with nested list:

{ "_id" : NumberLong(9), "description" : "eq-test", "equipment" : "14500", "equipmentSpecificationProperties" : [ null, null ], "version" : 0 }
1
I'd remove hasMany from your Isa95EquipmentSpecification classinjecteer
@injecteer I removed hasMany but got the some resultskioppetto
"equipmentSpecificationProperties" : [ null, null ] this makes me think, that the objects are being persisted as sub-docs. you should be able to see the equipmentSpecificationProperties array with some elements in a mongo-browserinjecteer
If I call spec.save() prop1/prop2 are not persisted. If I explicity call prop1.save() and prop2.save() they are stored in the Isa95EquipmentSpecificationProperty collection. Both cases I find "equipmentSpecificationProperties" : [ null, null ]. I query mongodb using mongo shell and I expect to find at least some refs in equipmentSpecificationPropertiesskioppetto
no, you shouldn't call prop.save(). what do you mean by find at least some refs in equipmentSpecificationProperties? there should not be any refs at all, the props are to be saved directly as sub-docsinjecteer

1 Answers

0
votes

specify the list type:

List<Isa95EquipmentSpecificationProperty> equipmentSpecificationProperties