【问题标题】:How to convert org.neo4j.rest.graphdb.entity.RestNode to Spring Data Neo4j Node Entity如何将 org.neo4j.rest.graphdb.entity.RestNode 转换为 Spring Data Neo4j 节点实体
【发布时间】:2013-03-06 13:58:39
【问题描述】:

我正在使用 Spring Data for Neo4j 来访问 Neo4j 图表。我有一个带有注释查询的UserRepository,如下所示:

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);
}

我试图从图表中检索到的是去过类似地方的用户列表,对于每个用户,他们去过的常见地方是什么。该方法将返回Map&lt;String, Object&gt; 的列表。每个地图都将包含 similar_user、locations 和 count 之类的键。

从调试语句中我可以看到similar_user 是org.neo4j.rest.graphdb.entity.RestNode 的一个实例。有什么方法可以将其转换为我的 Spring Data 节点实体 com.abc.graph.entity.User?

【问题讨论】:

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


    【解决方案1】:

    您可以使用以下方法手动完成:

    template.createEntityFrom[Stored]State(userNode[,User.class)

    或者您定义一个Iterable 或Collection 作为结果,该接口使用@MapResult 注释,并为三列提供getter。它要么自动将 getter 名称映射到结果列,要么您可以提供要映射到的名称。

    @MapResult
    interface SimilarUser {
    
       @ResultColumn("count") int getCount();
       @ResultColumn("similar_user") User getUser();
       @ResultColumn("locations") Collection<Location> getLocations();
    }
    

    【讨论】:

    • 谢谢! @MapResult 注释有效。但是我在调​​用getLocations() 方法时遇到了这个异常:org.springframework.data.neo4j.conversion.QueryResultBuilder$1 cannot be cast to java.util.Collection 知道吗?
    • 嗯,好点,你能用@ResultColumn("locations") Iterable&lt;Location&gt; getLocations();
    • 我在尝试调用Iterator 的next() 方法时遇到另一个异常:java.lang.RuntimeException: Cannot extract single value from Iterable with more than one elements.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    相关资源
    最近更新 更多