Search code examples
javaspringpostgresqlspring-bootlinkedhashmap

Map.remove("x") removes from relevant "x" in Postgres Json column


I'm reading data from Postgres DB and then I need to remove one part of JSON data from the red data. But this has an impact on the database level because this even removes data from the DB level.

remove method

private static Map<String,Object> removePrivate(Object key) {
        try {
            Map<String, Object> keyMap = (Map<String, Object>) key;
            keyMap.remove("d");
            return keyMap;
        } catch (Exception ex) {
            log.warn("no private component 'd' in private key",ex);
            return null;
        }
    }

The remove method is used here.

public List<Key> getKeys(URI controller, URI url, String type, String purpose, String reference, Long limit, boolean exportPrivate) {

        List<Key> keyList = new ArrayList<>();
        List<com.xxx.model.Key> keys = keyRepository.getKeys(controller, url, type, purpose, reference, limit);
        if (keys == null || keys.isEmpty()) throw new BadRequestException("No keys found for given settings");
        keys.forEach(key -> {
            try {
                Key k = modelMapper.map(key, Key.class);
                keyList.add(k);
            } catch (Exception e) {
                log.error("Parse exception {}", e.getMessage(),e);
                throw new BadRequestException("Invalid key format",e);
            }
        });

        if (!exportPrivate) keyList.forEach(key -> key.setKey(removePrivate(key.getKey())));
        return keyList;
    }

Is there any clear reason why removing the property "d" from the map has an effect on the database level and also it removes the "d" component from "keys" List, but it should remove only from "keyList" List.


Solution

  • The cause for the issue is @Trasactional. when I remove this the issue removed. But it will be nice if someone can explain how @Trasactional can cause such an issue.