@Test public void testMerge() throws Exception { HostConfigMapping source = createEntity(1L, "h1", "global", "v1"); HostEntity hostEntity = hostDAO.findByName("h1"); Set<HostConfigMapping> target = hostConfigMappingDAO.findByType(1L, hostEntity.getHostId(), "global"); Assert.assertEquals("Expected one result", 1, target.size()); HostConfigMapping toChange = null; for (HostConfigMapping item: target) { Assert.assertEquals("Expected version 'v1'", source.getVersion(), item.getVersion()); Assert.assertEquals("Expected selected flag 1", 1, (int)item.getSelected()); toChange = item; toChange.setSelected(0); } hostConfigMappingDAO.merge(toChange); target = hostConfigMappingDAO.findByType(1L, hostEntity.getHostId(), "global"); Assert.assertEquals("Expected one result", 1, target.size()); for (HostConfigMapping item: target) { Assert.assertEquals("Expected version 'v1'", source.getVersion(), item.getVersion()); Assert.assertEquals("Expected selected flag 0", 0, (int)item.getSelected()); } }
@Transactional public HostConfigMapping merge(HostConfigMapping hostConfigMapping) { populateCache(); Long hostId = hostConfigMapping.getHostId(); if (hostId != null) { Set<HostConfigMapping> set; if (hostConfigMappingByHost.containsKey(hostId)) { set = hostConfigMappingByHost.get(hostId); } else { set = new HashSet<HostConfigMapping>(); hostConfigMappingByHost.put(hostId, set); } set.remove(hostConfigMapping); set.add(hostConfigMapping); entityManagerProvider.get().merge(buildHostConfigMappingEntity(hostConfigMapping)); } return hostConfigMapping; }
HostConfigMappingDAO { @Transactional public HostConfigMapping merge(HostConfigMapping hostConfigMapping) { populateCache(); Long hostId = hostConfigMapping.getHostId(); if (hostId != null) { Set<HostConfigMapping> set; if (hostConfigMappingByHost.containsKey(hostId)) { set = hostConfigMappingByHost.get(hostId); } else { set = new HashSet<HostConfigMapping>(); hostConfigMappingByHost.put(hostId, set); } set.remove(hostConfigMapping); set.add(hostConfigMapping); entityManagerProvider.get().merge(buildHostConfigMappingEntity(hostConfigMapping)); } return hostConfigMapping; } }
HostConfigMappingDAO { @Transactional public HostConfigMapping merge(HostConfigMapping hostConfigMapping) { populateCache(); Long hostId = hostConfigMapping.getHostId(); if (hostId != null) { Set<HostConfigMapping> set; if (hostConfigMappingByHost.containsKey(hostId)) { set = hostConfigMappingByHost.get(hostId); } else { set = new HashSet<HostConfigMapping>(); hostConfigMappingByHost.put(hostId, set); } set.remove(hostConfigMapping); set.add(hostConfigMapping); entityManagerProvider.get().merge(buildHostConfigMappingEntity(hostConfigMapping)); } return hostConfigMapping; }  }
HostConfigMappingDAO { @Transactional public HostConfigMapping merge(HostConfigMapping hostConfigMapping) { populateCache(); Long hostId = hostConfigMapping.getHostId(); if (hostId != null) { Set<HostConfigMapping> set; if (hostConfigMappingByHost.containsKey(hostId)) { set = hostConfigMappingByHost.get(hostId); } else { set = new HashSet<HostConfigMapping>(); hostConfigMappingByHost.put(hostId, set); } set.remove(hostConfigMapping); set.add(hostConfigMapping); entityManagerProvider.get().merge(buildHostConfigMappingEntity(hostConfigMapping)); } return hostConfigMapping; }  @Transactional void create(HostConfigMapping hostConfigMapping); @Transactional HostConfigMapping merge(HostConfigMapping hostConfigMapping); @RequiresSession Set<HostConfigMapping> findByType(final long clusterId, Long hostId, final String type); @RequiresSession HostConfigMapping findSelectedByType(final long clusterId,
      Long hostId, final String type); @RequiresSession Set<HostConfigMapping> findSelected(final long clusterId, Long hostId); @RequiresSession Set<HostConfigMapping> findSelectedByHosts(Collection<Long> hostIds); @RequiresSession Map<String, List<HostConfigMapping>> findSelectedHostsByTypes(final long clusterId,
                                                                             Collection<String> types); @RequiresSession List<HostConfigMappingEntity> findAll(); @Transactional void removeByHostId(Long hostId); @Transactional void removeByClusterAndHostName(final long clusterId, String hostName); HostConfigMappingEntity buildHostConfigMappingEntity(HostConfigMapping hostConfigMapping); HostConfigMapping buildHostConfigMapping(
      HostConfigMappingEntity hostConfigMappingEntity); }
HostConfigMappingDAO { @Transactional public HostConfigMapping merge(HostConfigMapping hostConfigMapping) { populateCache(); Long hostId = hostConfigMapping.getHostId(); if (hostId != null) { Set<HostConfigMapping> set; if (hostConfigMappingByHost.containsKey(hostId)) { set = hostConfigMappingByHost.get(hostId); } else { set = new HashSet<HostConfigMapping>(); hostConfigMappingByHost.put(hostId, set); } set.remove(hostConfigMapping); set.add(hostConfigMapping); entityManagerProvider.get().merge(buildHostConfigMappingEntity(hostConfigMapping)); } return hostConfigMapping; }  @Transactional void create(HostConfigMapping hostConfigMapping); @Transactional HostConfigMapping merge(HostConfigMapping hostConfigMapping); @RequiresSession Set<HostConfigMapping> findByType(final long clusterId, Long hostId, final String type); @RequiresSession HostConfigMapping findSelectedByType(final long clusterId,
      Long hostId, final String type); @RequiresSession Set<HostConfigMapping> findSelected(final long clusterId, Long hostId); @RequiresSession Set<HostConfigMapping> findSelectedByHosts(Collection<Long> hostIds); @RequiresSession Map<String, List<HostConfigMapping>> findSelectedHostsByTypes(final long clusterId,
                                                                             Collection<String> types); @RequiresSession List<HostConfigMappingEntity> findAll(); @Transactional void removeByHostId(Long hostId); @Transactional void removeByClusterAndHostName(final long clusterId, String hostName); HostConfigMappingEntity buildHostConfigMappingEntity(HostConfigMapping hostConfigMapping); HostConfigMapping buildHostConfigMapping(
      HostConfigMappingEntity hostConfigMappingEntity);  }