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)
]
I ended up by creating code that traverses the JSON object and creates domain instances.