Source:
public void shouldSaveOrderGroupAlongWithOrders() throws Exception { executeDataSet(ORDER_SET); Encounter encounter = new Encounter(); encounter.setPatient(Context.getPatientService().getPatient(3)); encounter.setEncounterType(Context.getEncounterService().getEncounterType(1)); encounter.setEncounterDatetime(new Date()); Context.getEncounterService().saveEncounter(encounter); Integer encounterId = Context.getEncounterService().getEncounterByUuid(encounter.getUuid()).getId(); OrderSet orderSet = Context.getOrderSetService().getOrderSetById(2000); OrderGroup orderGroup = new OrderGroup(); orderGroup.setOrderSet(orderSet); orderGroup.setEncounter(Context.getEncounterService().getEncounter(encounterId)); Order newOrder1 = new OrderBuilder().withAction(Order.Action.NEW).withPatient(3).withConcept(1000) .withCareSetting(1).withOrderer(1).withEncounter(encounterId).withDateActivated(new Date()).withOrderType(17) .withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).build(); newOrder1.setOrderGroup(orderGroup); Order newOrder2 = new OrderBuilder().withAction(Order.Action.NEW).withPatient(3).withConcept(1001) .withCareSetting(1).withOrderer(1).withEncounter(encounterId).withDateActivated(new Date()).withOrderType(17) .withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).build(); newOrder2.setOrderGroup(orderGroup); encounter.addOrder(newOrder1); encounter.addOrder(newOrder2); Context.getEncounterService().saveEncounter(encounter); Context.flushSession(); List<Order> orders = new ArrayList<Order>(); orders.addAll(Context.getEncounterService().getEncounterByUuid(encounter.getUuid()).getOrders()); assertEquals("Only one orderGroup gets saved for both the orders ",orders.get(0).getOrderGroup().getId(), orders.get(1).getOrderGroup().getId()); }

Target:
public void shouldSaveOrderGroupAlongWithOrders() throws Exception { executeDataSet(ORDER_SET); Encounter encounter = new Encounter(); encounter.setPatient(Context.getPatientService().getPatient(3)); encounter.setEncounterType(Context.getEncounterService().getEncounterType(1)); encounter.setEncounterDatetime(new Date()); Context.getEncounterService().saveEncounter(encounter); Integer encounterId = Context.getEncounterService().getEncounterByUuid(encounter.getUuid()).getId(); OrderSet orderSet = Context.getOrderSetService().getOrderSet(2000); OrderGroup orderGroup = new OrderGroup(); orderGroup.setOrderSet(orderSet); orderGroup.setPatient(encounter.getPatient()); orderGroup.setEncounter(Context.getEncounterService().getEncounter(encounterId)); Order firstOrderWithOrderGroup = new OrderBuilder().withAction(Order.Action.NEW).withPatient(3).withConcept(1000) .withCareSetting(1).withOrderer(1).withEncounter(encounterId).withDateActivated(new Date()) .withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()) .withOrderGroup(orderGroup).build(); Order secondOrderWithOrderGroup = new OrderBuilder().withAction(Order.Action.NEW).withPatient(3).withConcept(1001) .withCareSetting(1).withOrderer(1).withEncounter(encounterId).withDateActivated(new Date()) .withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()) .withOrderGroup(orderGroup).build(); encounter.addOrder(firstOrderWithOrderGroup); encounter.addOrder(secondOrderWithOrderGroup); Context.getEncounterService().saveEncounter(encounter); Context.flushSession(); List<Order> orders = new ArrayList<Order>(); orders.addAll(Context.getEncounterService().getEncounterByUuid(encounter.getUuid()).getOrders()); assertNotNull("OrderGroup is saved", orders.get(0).getOrderGroup()); assertEquals("OrderGroup isa same for both the orders ", true, orders.get(0).getOrderGroup().equals( orders.get(1).getOrderGroup())); }