Reputation: 5127
I'm using Spring Data for Neo4j to access Neo4j graph. I have a UserRepository
with an annotated query as below:
package com.abc.graph.repository;
import java.util.List;
import java.util.Map;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.data.neo4j.repository.NamedIndexRepository;
import org.springframework.data.neo4j.repository.RelationshipOperationsRepository;
import com.abc.graph.entity.User;
public interface UserRepository extends GraphRepository<User>, NamedIndexRepository<User>,
RelationshipOperationsRepository<User> {
public User findById(String id);
public Page<User> findByNameLike(String name, Pageable page);
@Query("START user=node:User(id={0}) " +
"MATCH user-[:VISITS]->(location)<-[:VISITS]-(similar_user) " +
"RETURN similar_user, collect(location) as locations, count(*) as count " +
"ORDER BY count desc ")
public List<Map<String, Object>> findSimilarUsersByPlaceVisited(String userId);
}
What I am trying to retrieve from the graph is a list of users who have been to the similar places and for each user, what are the common places they have been to. The method will return a list of Map<String, Object>
. Each map will contains key like similar_user
, locations
and count
.
From debug statement I can see that similar_user
is an instance of org.neo4j.rest.graphdb.entity.RestNode
. Is there any way to convert it to my Spring Data node entity which is com.abc.graph.entity.User
?
Upvotes: 0
Views: 1206
Reputation: 41676
You can do it manually by using:
template.createEntityFrom[Stored]State(userNode[,User.class)
Or you define as result an Iterable
or Collection
of an interface annotated with @MapResult
with getters for the three columns. It either automatically maps the getter names to result columns or you can provide a name to map to.
@MapResult
interface SimilarUser {
@ResultColumn("count") int getCount();
@ResultColumn("similar_user") User getUser();
@ResultColumn("locations") Collection<Location> getLocations();
}
Upvotes: 3