2
votes

I just started to use Neo4j with Spring Data and I'm not able to recover graph objects and convert them back to domain objects. I must say I have no previous experience in that kind of databases.

In that way, I'm using Spring Data repositories. For standard queries the repository code is auto-generated but I would like to define some custom methods, so I created my custom repository as explained here.

For example, I would like to be able to update a certain property value (currentValue property in this case) from a given edge between two certain nodes (searchByUserName is a previously defined index in my node entity which represents a user). I'm using the query method from the Neo4j template in my custom repository implementation as follows:

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 */
    }

The cypher query returns an "edge" between two users, where its relationship type is "FOLLOWS", simulating a Twitter users network. I have no idea how to convert this QueryResult object back to my RelationshipEntity object. Is that possible?

1

1 Answers

5
votes

Just use the result-dsl: http://static.springsource.org/spring-data/data-graph/snapshot-site/reference/html/#d5e1118

relationships.to(MyRelationshipEntity.class)

will return you a Result<MyRelationshipEntity> which is an Iterable