首页 > 其他 > 详细

Neo4J:结果返回多个自选字段

时间:2020-06-04 21:04:58      阅读:101      评论:0      收藏:0      [点我收藏+]

最近项目中使用Neo4j查询数据,需要选择多个数据返回例如:

MATCH (user:ShortouchUser)-[r:HAS_ACCOUNT]->(xnsf)  return user.name as name,user.sex as sex,user.email as email,user.identity_card as identity_card, user.mobile_phone as mobile_phone, xnsf.name as twitterName,xnsf.facebookName as facebookName, id(user) as nodeid
Repository:
@EnableNeo4jRepositories
public interface ShortTouchUserToUserFaceBookReponsitory extends Neo4jRepository<ShortTouchUserToUserFaceBook, Long> {

    @Query(value = "MATCH (user:ShortouchUser)-[r:HAS_ACCOUNT]->(xnsf)  return user.name as name,user.sex as sex,user.email as email,user.identity_card as identity_card, user.mobile_phone as mobile_phone, xnsf.name as twitterName,xnsf.facebookName as facebookName, id(user) as nodeid",
        countQuery = "MATCH (user:ShortouchUser)-[r:HAS_ACCOUNT]->(xnsf)  return count(user)")
    Page<ExportSearchData> searchDataFromNeo4J(Pageable pageable);

}

entity:

public class ExportSearchData {
    private String name;
    private String sex;
    private String email;
    private String identity_card;
    private String mobile_phone;
    private String twitterName;
    private String facebookName;
    private Long nodeid;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getIdentityCard() {
        return identity_card;
    }
    public void setIdentityCard(String identity_card) {
        this.identity_card = identity_card;
    }
    public String getMobilePhone() {
        return mobile_phone;
    }
    public void setMobilePhone(String mobile_phone) {
        this.mobile_phone = mobile_phone;
    }
    public String getTwitterName() {
        return twitterName;
    }
    public void setTwitterName(String twitterName) {
        this.twitterName = twitterName;
    }
    public String getFacebookName() {
        return facebookName;
    }
    public void setFacebookName(String facebookName) {
        this.facebookName = facebookName;
    }
    public Long getNodeid() {
        return nodeid;
    }
    public void setNodeid(Long nodeid) {
        this.nodeid = nodeid;
    }
}

到这里,其实和一般的查询一样,但是在运行的时候,会报错提示:

java.lang.RuntimeException: Scalar response queries must only return one column. Make sure your cypher query only returns one item.

查询了资料,需要给Entity增加标记@QueryResult。

@QueryResult
public class ExportSearchData {
...
}

但是另外一个问题出现:

At present, only @Result types that are discovered by the domain entity package scanning can be mapped

找了很多地方,最后发现了问题,是由于返回ExportSearchData没有和neo4j其他的entity放在同一个文件路径下 ,否则就会报错。

特此记录一下。

 

参考资料:https://stackoverflow.com/questions/42024716/how-to-get-custom-results-using-spring-data-jpa-using-neo4j?r=SearchResults

Neo4J:结果返回多个自选字段

原文:https://www.cnblogs.com/Destiny-zhl/p/13045604.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!