【问题标题】:Map cypher query result to domain object将密码查询结果映射到域对象
【发布时间】:2013-03-21 21:50:01
【问题描述】:

我刚开始将 Neo4j 与 Spring Data 一起使用,但我无法恢复图形对象并将它们转换回域对象。我必须说我以前没有这种数据库的经验。

这样,我正在使用 Spring Data 存储库。对于标准查询,存储库代码是自动生成的,但我想定义一些自定义方法,因此我按照here 的说明创建了自定义存储库。

例如,我希望能够从两个特定节点之间的给定边更新某个属性值(在这种情况下为 currentValue 属性)(searchByUserName 是我的节点实体中先前定义的索引,它代表一个用户)。我在自定义存储库实现中使用来自 Neo4j 模板的查询方法,如下所示:

public class TwitterUserRepositoryImpl implements TwitterUserRepositoryCustom{

    @Autowired
    private Neo4jOperations neo4jTemplate;

public void updateRelationshipValueByUserName(
            String userAUserName, String userBUserName, double value){
        HashedMap params = new HashedMap();
        params.put("userAUserName", userAUserName);
        params.put("userBUserName", userBUserName);
        params.put("value", value);
        String query = "START x=node:searchByUserName(userName = {userAUserName}), " +
                        "y=node:searchByUserName(userName = {userBUserName})" +
                        " MATCH (x)-[r:FOLLOWS]->(y)" +
                        " SET r.currentValue = {value}" +
                        " RETURN r";
        Result<Map<String, Object>> relationships = neo4jTemplate.query(query, params);
        /* let's try to recover the relationship entity and do some more stuff */
    }

密码查询返回两个用户之间的“边缘”,其关系类型为“FOLLOWS”,模拟 Twitter 用户网络。我不知道如何将此 QueryResult 对象转换回我的 RelationshipEntity 对象。这可能吗?

【问题讨论】:

    标签: neo4j spring-data-neo4j


    【解决方案1】:

    只需使用结果-dsl:http://static.springsource.org/spring-data/data-graph/snapshot-site/reference/html/#d5e1118

    relationships.to(MyRelationshipEntity.class)
    

    将返回一个Result&lt;MyRelationshipEntity&gt;,即Iterable

    【讨论】:

    • 这正是我想要的。非常感谢,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多