Neo4J:結果返回多個自選字段


最近項目中使用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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM