2016-07-14 32 views
1

雖然使用Spring引導對Spring Neo4j編寫了poc,但我發現它看起來是Bolt驅動程序和Http驅動程序之間的不一致行爲。基本上,在保存2個節點之間的豐富關係之後,使用Bolt驅動程序時,測試無法加載它,但使用Http驅動程序嘗試時完全相同的測試會成功。Spring Neo4j - 當Http驅動程序成功執行時,螺栓驅動程序無法加載關係

示例項目可以從github

下載這是一個非常基本的/直接的測試,唯一的先決條件是,你將需要有Neo4j的3啓用了螺栓接頭安裝。

正如安德烈建議請找代碼的有關章節如下:

@NodeEntity(label = "Person") 
public class Person { 
    private Long id; 
    private String firstname; 
    private String lastname; 

    @Relationship(type = "HAS_CONTACT", direction = Relationship.INCOMING) 
    private Contact contact; 

    // getters and setters here ......... 
} 

@NodeEntity(label = "BankAccount") 
public class BankAccount { 

    private Long id; 
    private Integer balance; 

    @Relationship(type = "HAS_CONTACT") 
    private List<Contact> contacts = new ArrayList<>(); 

    // getters and setters here ......... 
} 

@RelationshipEntity(type = "HAS_CONTACT") 
public class Contact { 

    public Contact() { 
    } 

    public Contact(BankAccount bankAccount, Person person) { 
     this.bankAccount = bankAccount; 
     this.person = person; 

     this.bankAccount.getContacts().add(this); 
     this.person.setContact(this); 
    } 

    private Long id; 

    @StartNode 
    private BankAccount bankAccount; 

    @EndNode 
    private Person person; 

    private String email; 
    private String phoneNumber; 

    // getters and setters here ......... 
} 

@Repository 
public interface ContactRepository extends GraphRepository<Contact> { 

    @Query("MATCH (a:BankAccount)-[r:HAS_CONTACT]->(:Person) " + 
      "WHERE ID(a)={accountId} " + 
      "RETURN r") 
    Iterable<Contact> findByAccountId(@Param("accountId") Long accountId); 
} 

節省1個帳戶後,1人以及它們之間1種的接觸關係,下面的查詢是失敗的一個:

Iterable<Contact> contacts = contactRepository.findByAccountId(accountId); 

// this assertion will Fail for the BOLT driver, however, it will Succeed for the HTTP driver 
// if the accountRepository.findOne(accountId) statement is executed before calling 
// contactRepository.findByAccountId(accountId) then the test will also succeed for the BOLT driver 
assertThat(size(contacts), is(1)); 
+0

你應該在你的問題中包含相關部分的代碼。最終,外部鏈接不可用,並阻止潛在用戶從您的帖子中受益。 – Andrej

+0

新增,感謝您的建議 – artemisian

回答

1

請參閱從Neo4j的團隊對issue響應如下:

釷Ë原因關係的實體不能映射是因爲開始和結束的節點與此查詢不可用:

@Query("MATCH (a:BankAccount)-[r:HAS_CONTACT]->(p:Person) " + 
      "WHERE ID(a)={accountId} " + 
      "RETURN r,") 
    Iterable<Contact> findByAccountId(@Param("accountId") Long accountId); 

您需要返回這些節點能夠構建一個有效的 關係的實體,像這樣:

@Query("MATCH (a:BankAccount)-[r:HAS_CONTACT]->(p:Person) " + 
      "WHERE ID(a)={accountId} " + 
      "RETURN r,a,p") 
    Iterable<Contact> findByAccountId(@Param("accountId") Long accountId); 

這是HTTP端點的副作用,它允許通過 - 無論如何這個端點返回 開始和結束節點。請參閱 http://graphaware.com/neo4j/2016/04/06/mapping-query-entities-sdn.html 瞭解更多信息。

相關問題