JsonSetConverter implements AttributeConverter<Set, String> { @Override public String convertToDatabaseColumn(final Set value) { try { return serializer.writeValueAsString(value); } catch (final JsonProcessingException e) { log.error("Unable to parse Set - {}. Error - {}", value, e.getMessage(), e); throw new FabricManagerException(e); } } JsonSetConverter();  JsonSetConverter(final ObjectMapper serializer); @Override String convertToDatabaseColumn(final Set value); @Override Set convertToEntityAttribute(final String value);  }
@Test public void shouldConvertStringToDatabaseColumn() { JsonSetConverter converter = new JsonSetConverter(); Set<String> set = Sets.newHashSet(); set.add("element_1"); set.add("element_2"); String json = converter.convertToDatabaseColumn(set); boolean valid = (json.equalsIgnoreCase("[\"element_1\",\"element_2\"]") || json.equalsIgnoreCase("[\"element_2\",\"element_1\"]")); assertTrue(valid); }
@Test public void shouldConvertLongToDatabaseColumn() { JsonSetConverter converter = new JsonSetConverter(); Set<Long> set = Sets.newHashSet(); set.add(1L); set.add(2L); assertEquals(converter.convertToDatabaseColumn(set), "[1,2]"); }