Uploaded image for project: 'OpenMRS Core'
  1. OpenMRS Core
  2. TRUNK-2535

Using merge in service layer



    • Type: New Feature
    • Status: Design
    • Priority: TBD
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Data Model
    • Labels:
    • Complexity:


      Using merge in our service layer would allow us to get rid off NonUniqueObjectException "a different object with the same identifier value was already associated with the session." in many places without special tricks like clearing session.

      The difference is that saveOrUpdate reattaches the given object to the session whereas merge returns an attached version of the given object e.g.

      Concept c1 = new Concept(1);
      Concept c11 = new Concept(1);
      sf.saveOrUpdate(c1); //c1 is now attached to the session
      sf.evict(c1); //we need to evict c1 from the sesssion in order to save c11 since they have same ids of value 1
      sf.saveOrUpdate(c11); //c1 is overwritten by c11, c11 is now attached to the session
      Concept c2 = new Concept(2);
      Concept c22 = new Concept(2);
      Concept newC2 = sf.merge(c2); //newC2 is attached to the session whereas c2 remains detached
      Concept newC22 = sf.merge(c22); //c2 is overwritten by c22, newC22 == newC2 is true which is guaranteed by Hibernate

      It'd be a significant change to the API behavior, if we decided to change save methods itself, yet it wouldn't require us to change save method signatures since they already return an attached object. The problem would only arise, if someone didn't care for the returned object and used his own object assuming it is attached.

      However, we could simply add merge methods to the API. At this point we could also consider having a generic BaseOpenmrsService class that would provide us with simple CRUD (save, merge, purge, retire, getById, getByUuid) operations instead of reimplementing them all the time. It's a commonly used pattern for daos and services. An example is described here http://community.jboss.org/wiki/GenericDataAccessObjects.

        Gliffy Diagrams




              raff Rafal Korytkowski
              0 Vote for this issue
              5 Start watching this issue