Saving Patient and then calling Context.getPatientService().getAllPatients() throws Exception

Hi all,

This could be a simple Hibernate error but its my first time hitting. Run this unit test to reproduce.

@test 
public void dummyTest() {
    //Create yr Patient Object
    Patient patient = new Patient(); //Add related metadata to the patient like name.
    //Save the patient
    Context.getPatientService().savePatient(patient);
    //Get all the Patient objects
    Context.getPatientService().getAllPatients(); //Oppppss, throws 'java.lang.ClassCastException'
}

Full error log :-

https://pastebin.com/4W3FkCz4

cc: @ssmusoke , @dkayiwa

If you are asking for help about a runtime error, having a test that does not even compile, is not a good practice. Expecting some one to Add related metadata to the patient like name. as per your comments shows that you are too lazy to even help yourself.

In summary, people are very busy. If you need their attention, make them do as little as possible to get to what you want.

I’m sorry, I didn’t review the code before posting

@Test
public void savePatients_shaouldSavePatients() {

	Patient patient = new Patient();
	PersonName pName = new PersonName();
	pName.setGivenName("Samuel");
	pName.setMiddleName("Male");
	pName.setFamilyName("Nsereko");
	
	patient.addName(pName);
	
	PersonAddress address = new PersonAddress();
	address.setAddress1("Masaka");
	
	patient.addAddress(address);
	patient.setBirthdate(new Date());
	patient.setGender("male");
	
	PatientIdentifier identifier = new PatientIdentifier();
	identifier.setIdentifier("839");
	identifier.setLocation(new Location(1));
	identifier.setIdentifierType(Context.getPatientService().getAllPatientIdentifierTypes().get(0));
	PatientIdentifier identifier2 = new PatientIdentifier();
	identifier2.setIdentifier("890");
	identifier2.setLocation(new Location(1));
	identifier2.setIdentifierType(Context.getPatientService().getAllPatientIdentifierTypes().get(0));
	identifier2.setPreferred(true);
	Set<PatientIdentifier> ids = new LinkedHashSet<>();
	ids.add(identifier);
	ids.add(identifier2);
	patient.setIdentifiers(ids);

	Context.getPatientService().savePatient(patient);
	Context.getPatientService().getAllPatients(); //This throws the Exception
	
}

Error log :- java.lang.ClassCastException: java.base/java.util.LinkedHashSet cannot be cast t - Pastebin.com

I’m soo sorry @dkayiwa

Doesn’t that test method name result into errors of its own?

:grimacing:

Looks like this is the root cause of the error.

True dat. And can be fixed by changing to Set<PatientIdentifier> ids = new TreeSet<>();

But the method name is also not well written.