0

I have a model with 3 classes, A, Parent and Child. A hasMany Parent, and Child extends Parent.

I need to handle complex logic on the UI to create the model, and decided to POST a JSON object with the same structure of my domain model. Now, I have an instance of A that has some instances of Child associated, that in a javascript object on the client side.

When I POST the javascript object, serializing it to a JSON string, the controller receives it ok, then I do: def a = new A(request.JSON);

The A instance is created, but on the hasMany relationship it has instances of Parent instead of instances of Child, so I lose all the attribute values from Child.

How can I tell Grails to create instances of Child instead of instances of Parent?

UPDATE

The request.JSON structure received by the controller looks like this:

[
 name: name,
 group: none,
 where: [
    [
        codeOperand: in_list,
        archetypeId: openEHR-EHR-EVALUATION.problem_diagnosis.v1,
        codeValues: [
            aaa,
            bbb
        ],
        path: /data[at0001]/items[at0005]/value,
        class: Child1,
        terminologyIdValues: [
            cie
        ],
        rmTypeName: DV_CODED_TEXT,
        terminologyIdOperand: eq
    ],
    [
        magnitudeValues: [
            1,
            22
        ],
        archetypeId: openEHR-EHR-OBSERVATION.blood_pressure.v1,
        path: /data[at0001]/events[at0006]/data[at0003]/items[at0004]/value,
        unitsOperand: eq,
        class: Child2,
        magnitudeOperand: between,
        rmTypeName: DV_QUANTITY,
        unitsValues: [
            mmHg
        ]
    ]
 ]
]

On this case I have 2 classes Child1 and Child2 that inherits from Parent.

I've added the "class" attribute to the objects because I read that might solve the problem but it didn't.

When I print the objects associated to the A instance, I got the right number of instances, but all the classes are "Parent", as you can see, some values are binded, but are all attributes of the Parent class.

[
 (archetypeId: openEHR-EHR-EVALUATION.problem_diagnosis.v1,
  path: /data[at0001]/items[at0005]/value,
  rmTypeName: DV_CODED_TEXT,
  class: Parent),
 (archetypeId: openEHR-EHR-OBSERVATION.blood_pressure.v1,
  path: /data[at0001]/events[at0006]/data[at0003]/items[at0004]/value, 
  rmTypeName: DV_QUANTITY,
  class: Parent)
]
GavinBrelstaff
  • 3,016
  • 2
  • 21
  • 39
Pablo Pazos
  • 3,080
  • 29
  • 42
  • please post the json you are getting on your controller – dsharew Jul 10 '15 at 06:45
  • @DegenSharew I've added some samples. – Pablo Pazos Jul 11 '15 at 02:28
  • still it is hard to help with out seeing the actual class hierarchy but try putting pakcagename.class for the value of class in the json. eg. class: "com.apar.Child" – dsharew Jul 13 '15 at 06:41
  • Added the package, didn't work. It seems I need to do the binding by hand. The class hierarchy is described on the initial question. I just have different class names (A=Query, Parent=DataCriteria, Child1=DataCriteriaDV_CODED_TEXT, Child2=DataCriteriaDV_QUANTITY), the code is here: https://github.com/ppazos/cabolabs-ehrserver/tree/attr_type_based_criteria/grails-app/domain/query – Pablo Pazos Jul 17 '15 at 04:24

1 Answers1

0

I ended up by creating code that traverses the JSON object and creates domain instances.

Pablo Pazos
  • 3,080
  • 29
  • 42