Saving new Entities + Updating Existing Ones using UUID only

I currently have a situation where I am passing entities with a pre-set UUID only, and I would like the following to happen:

  1. If the uuid does not exist save the entity

  2. If the uuid exists, then update the existing entity in the database with the data passed from the new object.

Is there any example code that shows this kind of functionality? Should I use metadata deploy to handle this?

I haven’t run the code, but something like this should work:

Location existingLocation = locationService.getLocationByUuid(location.getUuid());
if (existingLocation== null) {
  locationService.saveLocation(location);
} else {
  location.setId(existingLocation.getId());
  //evict existing location from Hibernate session as we will attempt to overwrite it
  Context.evictFromSession(existingLocation);
  locationService.saveLocation(location);
}

This is what Metadata Deploy is for, yes.

@raff How would this be done via Metadata deploy?

@ssmusoke, the recipe for “installing” metadata is well described at https://wiki.openmrs.org/display/docs/Metadata+Deploy+Module