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

时间:2015-10-08 07:47:26

标签: java neo4j spring-data spring-data-neo4j

当我填充包含的节点时,我需要保存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是枚举,Permission是另一个未使用@NodeEntity注释的类,     userAliasRepo扩展了GraphRepository&lt;&gt;

那我怎么能坚持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
        }
    }
}

2 个答案:

答案 0 :(得分:2)

as @ frant.hartm指定

  

到目前为止,还不支持除Set之外的其他集合类型   目前没有地图&gt;。

但是org.springframework.data.neo4j.fieldaccess.DynamicProperties可以用来代替Map,然后Map映射到node-attributes。唯一的权衡是它只支持原始的dataTypes和它们的corressponding数组。

@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

答案 1 :(得分:1)

3.x版本不支持此功能:

  

到目前为止,不支持除Set之外的其他集合类型,目前也不支持Map<RelationshipType,Set<NodeBacked>>

http://docs.spring.io/spring-data/data-neo4j/docs/3.4.0.RELEASE/reference/html/#reference_programming_model_relationships_relatedto