cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Removing entries from @Properties maps in OGM

Martin
Node Link

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;
  }
}
3 REPLIES 3

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.

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

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.