Supporting serialization of jsonb Postgres column using HAPI


I’m trying to implement the HAPI Plain server implementation on top of an existing PostgresQL database that stores a fhir resource as a jsonb column, but I’m not sure what to set the Java type of the column to be. I’m assuming this is a fairly common problem, but I can’t find many online guides detailing what I need to do. Here’s what I’ve tried:

Using the Patient model directly as the column type

import org.hl7.fhir.r4.model.Patient;
public class PatientModel implements Serializable {
    @Column(name = "resource", nullable = false)
    private Patient resource;

This resulted in org.hibernate.type.SerializationException: could not deserialize

Using hibernate-types
This results in Caused by: java.lang.IllegalArgumentException: Conflicting setter definitions for property "referenceElement": org.hl7.fhir.r4.model.Reference#setReferenceElement(1 params) vs org.hl7.fhir.r4.model.Reference#setReferenceElement(1 params) to which the solution described here was to not use Jackson and instead use the HAPI fhir serializers.

If I’m understanding what needs to happen, It seems like what needs to happen is to write some kind of converter that uses the HAPI FHIR parsers to convert a org.hl7.fhir.r4.model.Patient Java type into a PostgresQL JSONB and vice versa (and this needs to happen between the PatientModel and the database). In other words, implement the code that hibernate-types does but replace Jackson with the HAPI FHIR parser.

My questions are:

  1. Operationally, how do I get the PatientModel to have access to the FHIR context such that it can use the jsonParser? I see that the context getting instantiated at the instantiation of FhirRestfulServer but am not sure how to pass it around to the PatientModel for parsing.
  2. Once I have access to the fhir parser in that class, how do I implement the serialize/deserialize logic that I just described?
  3. Is this even the right approach? I could be completely off the mark here since I’m very new to Java (I’m used to python) so please let me know if there’s an easier way to do this!

Thanks! Help is much appreciated :pray: