Implementing Circular References with Hibernate in OpenMRS

Hello, all.

I am working on a CDS core module for OpenMRS. One functionality is to query a series of events when CDS was utilized and to serve them in JSON through a controller. My data model is shown in the following image. Basically, the table cdss_usage stores the events where cds was utilized. The table cdss_usage_recommendation stores each recommendation the the cds has provided. There is a many-to-one foreign key from cdss_usage_recommendation to cdss_usage. The intended stuctures is such that a cdss_usage can have many cdss_usage_recommendation, but cdss_usage_recommendationcan only have one ```cdss_usage``.

When I model this relationship in JPA, shown in the corresponding image. I set the @OneToMany and @JsonManagedReference on the recommendations field in CdssUsage and JsonBackReference, @ManyToOne and @JoinColumn on the usage field in CdssUsageRecommendation.

However, when I request the CdssUsage through a controller, the retriever enters an infinite recursive loop and does not return the proper representation of the usage.

Unless, I am missing something, I believe I modeled the entities correctly. Does anyone have an idea what is going wrong?

Thanks.

I’m guessing your serialization to json happens with something else than Jackson. It would help to see the stacktrace you eventually get to identify, which lib is involved. If it’s xstream then you could quickly try @XStreamOmitField on one end.

It may also be the very old version of Jackson being used (org.codehaus was retired years ago). If you can run a more up-to-date version of OpenMRS, you should have access to a more up-to-date version of Jackson.

Thank you for your suggestions. @ibacher I also tested it with Jackson (com.fasterxml.jackson) and it’s the same issue. @raff I’m not using Xstream.

Here is a link to the log. I truncated it due to memory restrictions.