Removing entries from @Properties maps in OGM

(Martin Folke Emdal) #1

Hi, I can't seem to remove a an entry from a map property of a node. For instance if the node Test with id "testId" has a map with the mapping "1" to "2" the code below will not remove that entry.

class Test {
  @Id String id;
  @Properties
  Map<String, String> map;
}

  // This code will not remove the "1" to "2" mapping from the database.
  public static void main(String[] args) {
    Test test = session.load(Test.class, "testId");
    test.map.remove("1");
    session.save(test);
  }
}

Edit:
Found a way to circumvent this by using an AttributeConverter instead of the @Properties notation. Since removing and adding things from lists seem to be easier with the OGM framework this code worked:

public class MyMapConverter implements AttributeConverter<Map<Integer,Integer>, List<Long>> {

  @Override
  public List<Long> toGraphProperty(Map<Integer, Integer> map) {
    if (map == null) {
      return null;
    }
    List<Long> list = new ArrayList<>();
    map.forEach((key, value) -> {
      list.add((((long) key) << 32 ) | value);
     }
    );
    return list;
  }

  @Override
  public Map<Integer,Integer> toEntityAttribute(List<Long> list) {
    Map<Integer,Integer> map = new HashMap<>();
    list.forEach(entry -> map.put((int) (entry >> 32), (int) (long) entry));
    return map;
  }
}
0 Likes

(Gerrit Meier) #2

Hi, could you tell us which version of Neo4j-OGM you are using? This was a bug/limitation we had before 3.1.6. Would be nice if you can upgrade this and check if the problem still exists within you application.

1 Like

(Martin Folke Emdal) #3

Hi, it seemed we were still on 3.1.5. Upgraded and tested with 3.1.6 now and it worked. Thanks!

0 Likes

(Godisdj007) #4

I am trying in 3.1.7 and its still not working.

        if (propValue == null)
			entity.getProperties().remove(propName);
		else
			entity.getProperties().put(propName, propValue);
		organisationRepository.save(entity);

Doesn't remove the map property on save.

0 Likes