编程语言
首页 > 编程语言> > 如何使用Spring Data Neo4j将Map(java.util.Map)对象持久保存在NodeEntity中?

如何使用Spring Data Neo4j将Map(java.util.Map)对象持久保存在NodeEntity中?

作者:互联网

我需要保存一个Map< Object,List< Object>>.当我填充包含类时,将保存该节点,但不会保存该地图.

这是我用于实体的代码

    @NodeEntity
    public class UserAlias{
        @GraphId
        private Long id;

        @Fetch
        private Map<IdentityType,List<Permission>> aliases;

        private String name;

    }
......
    userAliasRepo.save(userAlias)

IdentityType是一个Enum,Permission是另一个未使用@NodeEntity注释的类.
    userAliasRepo扩展了GraphRepository.

所以我该如何保存Map,我是Spring Data Neo4j版本3.3.0.RELEASE

我要实现的是将以下json与UserAlias NodeEntity相关联

{
    "name": "Bond",
    "permissions": {
        "Level5Acess": {
            "READ": false,
            "WRITE": false,
            "CREATE": false,
            "DEL": true
        },
        "Level4Acess": {
            "READ": false,
            "WRITE": false,
            "CREATE": false,
            "DEL": true
        },
        "Level1Acess": {
            "READ": true,
            "WRITE": true,
            "CREATE": true,
            "DEL": true
        },
        "Level0Acess": {
            "READ": true,
            "WRITE": true,
            "CREATE": true,
            "DEL": true
        }
    }
}

解决方法:

如@ frant.hartm指定

Other collection types than Set are not supported so far, also
currently NO Map>.

但是可以使用org.springframework.data.neo4j.fieldaccess.DynamicProperties代替映射,然后再映射到节点属性.唯一的权衡是,它仅支持原始数据类型及其对应的数组.

@NodeEntity
public class Person {
    @GraphId
    private Long graphId;

    private DynamicProperties personalProperties;
    public void setProperty(String key, Object value) {
        personalProperties.setProperty(key, value);
    }

    public Object getProperty(String key) {
        return personalProperties.getProperty(key);
    }
}
@Test
    public void testCreateOutsideTransaction() {
        Person p = new Person("James", 35);
        p.setProperty("s", "String");
        p.setProperty("x", 100);
        p.setProperty("pi", 3.1415);
        persist(p);
        assertEquals(3, IteratorUtil.count(p.getPersonalProperties().getPropertyKeys()));
        assertProperties(nodeFor(p));
        p.setProperty("s", "String two");
        persist(p);
        assertEquals("String two", nodeFor(p).getProperty("personalProperties-s"));
    }

http://forum.spring.io/forum/spring-projects/data/nosql/117145-spring-data-neo4j-how-to-map-java-util-map-string-string-field

标签:neo4j,spring-data,spring-data-neo4j,java
来源: https://codeday.me/bug/20191119/2037536.html