2016-07-14 34 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 了解更多信息。

相关问题