jpa ms sql json data type

Solutions on MaxInterview for jpa ms sql json data type by the best coders in the world

showing results for - "jpa ms sql json data type"
Antonio
14 Feb 2020
1@Converter
2public class JsonToMapConverter 
3                    implements AttributeConverter<String, Map<String, Object>> 
4{
5    private static final Logger LOGGER = LoggerFactory.getLogger(JsonToMapConverter.class);
6
7    @Override
8    @SuppressWarnings("unchecked")
9    public Map<String, Object> convertToDatabaseColumn(String attribute)
10    {
11        if (attribute == null) {
12           return new HashMap<>();
13        }
14        try
15        {
16            ObjectMapper objectMapper = new ObjectMapper();
17            return objectMapper.readValue(attribute, HashMap.class);
18        }
19        catch (IOException e) {
20            LOGGER.error("Convert error while trying to convert string(JSON) to map data structure.");
21        }
22        return new HashMap<>();
23    }
24
25    @Override
26    public String convertToEntityAttribute(Map<String, Object> dbData)
27    {
28        try
29        {
30            ObjectMapper objectMapper = new ObjectMapper();
31            return objectMapper.writeValueAsString(dbData);
32        }
33        catch (JsonProcessingException e)
34        {
35            LOGGER.error("Could not convert map to json string.");
36            return null;
37        }
38    }
39}
40