DemandResponseEventService { @Transactional(readOnly = false) public DemandResponseEvent update(DemandResponseEvent event, DemandResponseEventUpdateDto dto) { List<DemandResponseEventTarget> toKeep = new ArrayList<>(); List<DemandResponseEventTargetDto> toAdd = new ArrayList<>(); for (DemandResponseEventTargetDto updateTarget : dto.getTargets()) { boolean found = false; for (DemandResponseEventTarget target : event.getTargets()) { if (target.getTargetType().equals(updateTarget.getTargetType()) && target.getTargetId().equals(updateTarget.getTargetId())) { found = true; toKeep.add(target); } } if (!found) { toAdd.add(updateTarget); } } if (!toAdd.isEmpty()) { List<VenDemandResponseEvent> list = new ArrayList<>(); List<Ven> findVenForTarget = findVenForTarget(event, toAdd); for (Ven ven : findVenForTarget) { list.add(new VenDemandResponseEvent(event, ven)); } if (!list.isEmpty()) { venDemandResponseEventDao.saveAll(list); } } Set<DemandResponseEventTarget> actualSet = new HashSet<DemandResponseEventTarget>(event.getTargets()); actualSet.removeAll(toKeep); if (!actualSet.isEmpty()) { List<Ven> vens = findVenForTarget(event, new ArrayList<>(actualSet)); venDemandResponseEventDao.deleteByEventIdAndVenIn(event.getId(), vens); } if (dto.getPublished()) { event.getDescriptor().setModificationNumber(event.getDescriptor().getModificationNumber() + 1); } DemandResponseEvent partialUpdate = dtoMapper.map(dto, DemandResponseEvent.class); demandResponseEventSignalDao.deleteAll(event.getSignals()); partialUpdate.getSignals().forEach(sig -> sig.setEvent(event)); demandResponseEventSignalDao.saveAll(partialUpdate.getSignals()); event.getSignals().clear(); event.getTargets().clear(); event.getSignals().addAll(partialUpdate.getSignals()); event.getTargets().addAll(partialUpdate.getTargets()); event.setLastUpdateTimestamp(System.currentTimeMillis()); event.setPublished(partialUpdate.isPublished()); DemandResponseEvent save = demandResponseEventDao.save(event); if (dto.getPublished()) { this.distributeEventToPushVen(event); } return save; }  @PostConstruct void init(); @Transactional(readOnly = false) DemandResponseEvent create(DemandResponseEventCreateDto dto); @Transactional(readOnly = false) DemandResponseEvent update(DemandResponseEvent event, DemandResponseEventUpdateDto dto); void distributeEventToPushVen(DemandResponseEvent event); @Transactional(readOnly = false) DemandResponseEvent publish(DemandResponseEvent event); @Transactional(readOnly = false) DemandResponseEvent active(DemandResponseEvent event); @Transactional(readOnly = false) DemandResponseEvent cancel(DemandResponseEvent event); Optional<DemandResponseEvent> findById(Long id); List<DemandResponseEventSignal> getSignals(DemandResponseEvent event); boolean delete(Long id); void delete(Iterable<DemandResponseEvent> entities); boolean hasResponded(String venId, DemandResponseEvent event); void updateVenDemandResponseEvent(Long demandResponseEventId, Long modificationNumber, String venUsername,
			DemandResponseEventOptEnum venOpt); VenDemandResponseEvent getVenDemandResponseEvent(Long demandResponseEventId, String venUsername); DemandResponseEventOptEnum getVenDemandResponseEventOpt(Long demandResponseEventId, String venUsername); List<DemandResponseEvent> findToSentEventByVenUsername(String username); List<DemandResponseEvent> findToSentEventByVenUsername(String username, Long size); List<VenDemandResponseEvent> getVenDemandResponseEvent(Long demandResponseEventId); Page<DemandResponseEvent> search(List<DemandResponseEventFilter> filters, Long start, Long end); Page<DemandResponseEvent> search(List<DemandResponseEventFilter> filters, Long start, Long end, Integer page,
			Integer size);  }
@Test public void updateTest() { long count = venDemandResponseEventDao.count(); assertEquals(4L, count); Iterable<VenDemandResponseEvent> findAll = venDemandResponseEventDao.findAll(); Iterator<VenDemandResponseEvent> iterator = findAll.iterator(); while (iterator.hasNext()) { VenDemandResponseEvent next = iterator.next(); assertEquals(-1, next.getLastSentModificationNumber()); assertNull(next.getVenOpt()); } }