Can't retrive child Obs from a parent Obs in Obs Grouping

Hi devs:

I got a strange behavior in OpenMRS while retrieving saved Obs group.

I’m storing some data using Obs in OpenMRS. So, I want to create a Obs (lets say obsGroup), which can store some other Obs. Which means obsGroup contains the metadata of other Obs. After I retrieve the obsGroup, then I want to get other Obs which are contains in that obsGroup.

When I’m saving the obsGroup in L282, the resulting obsGroup work as it expected. Code: System.out.println(resultObs.getConcept().getDisplayString() + " Group members " + resultObs.isObsGrouping() + " > " + +groupMembers.size()); Output: Created result obs. true > 2 It says “this resultObs is a ObsGrouping and currently it has two members”.

When I trying to retrieve the value in L106, I can get the Obs grouping parent Obs. But it is not a grouping one and doesn’t have any member Obs. Code: System.out.println("Created result obs. " + resultObsGroup.isObsGrouping() + " > " + resultObsGroup .getGroupMembers().size()); Output:FHIR Diagnostic Report Result Group members false > 0 It says “this resultObsGroup is not a ObsGrouping and currently it hasn’t any members”.

As you can see, my current issue fulfills this test cases (because it only check in saved Obs group, not with a retrieved Obs group). There isn’t any stack traces.

Please, help me to figure out this issue :disappointed: . Ask me if you want more details of it. Thank you!

Seems to be Obs group Ids are not set in the DB (obs_group_id and value_group_id are null). Do I have to make sure any additional configuration before saving a Obs Grouping ? I checked the ‘is Set’ value while creating the Concept for Obs grouping parent.


@surangak helped me to figure out the problem.

This is happened due to setGroupMembers method fails. Look at the code.

I experienced same kind of issue previously as well. So, I come in to a dilemma, “setting data as a Set is not working in OpenMRS core”.

Start by creating a unit test to reproduce this. In your unit test, after saving the obs group, flush and clear the hibernate session. Then try retrieve it afresh from the database.

Heya! thanks @dkayiwa. @milan, it would be great if you can confirm that this is indeed a problem, and file a ticket on the problem… or even better, submit a patch :smile:

The member Obs does not have a reference to is Obsgroup, that’s why the relationship is not persisted. The setter works as expected. OTOH addGroupMember adds the member to the collection AND sets the reference.

Check the Obs mapping:

	<!-- many-to-one association from this obs to its parent grouping obs -->
	<!-- insert/update=false because the groupMembers set is doing the saving for us -->
	<many-to-one name="obsGroup" class="Obs" column="obs_group_id" insert="true" update="true"/>
	<!-- one-to-many association to all obs in this grouping -->
	<set name="groupMembers" inverse="true" cascade="all" order-by="obs_id"
	access = "field" batch-size="25">
		<key column="obs_group_id" />
		<one-to-many class="Obs" />