Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

NonUniqueObjectException

...

Here is another instance of code where one may see NonUniqueObjectException
Panel

Patient patient = null;
Person person = null;

try {
            patient = Context.getPatientService().getPatient(patientId);
}

catch(ClassCastException ex){
            person = Context.getPersonService().getPerson(patientId);
            patient = new Patient(person);
}

....

Context.getPatientService().savePatient(patient); // It throws NonUniqueObjectException

The Problem appears when ClassCastException is thrown.

...

A possible solution to this could be to explicitly evict one of the objects from the hibernate session. The best approach would be to add this call in a finally block to guarantee that always one person object remains in the hibernate session with the given patientId.

Panel

Patient patient = null;
Person person = null;

try {
            patient = Context.getPatientService().getPatient(patientId);

}

catch(ClassCastException ex){
            person = Context.getPersonService().getPerson(patientId);
            patient = new Patient(person);
}

finally {

// removing the person object from session making sure only one object with a given id is present

 if (patient!=null)
         Context.evictFromSession(person);

}

...

Context.getPatientService().savePatient(patient);

This will possibly resolve the error.