【问题标题】:Neo4j Same Entity Relationship persistence issueNeo4j 相同实体关系持久性问题
【发布时间】:2013-08-27 02:52:22
【问题描述】:

是否有可能请建议我如何去做.. 做一个相同的实体关系..

例如。 实体(类人)关联到实体(类人)。

代码:

@NodeEntity 
public class Person 
{ 
    @GraphId @GeneratedValue 
    private Long id; 

    @Indexed(indexType = IndexType.FULLTEXT, indexName = "searchByPersonName") 
    private String personName; 

    @Fetch @RelatedTo(type = "CONNECTS_TO", direction = Direction.BOTH) 
    private Set<ConnectedPersons> connectedPersons; 

    public ConnectedPersons connectsTo(Person endPerson, String connectionProperty) 
    {       
        ConnectedPersons connectedPersons = new ConnectedPersons(this, endPerson, connectionProperty); 
        this.connectedPersons.add(connectedPersons); //Null Pointer Here(connectedPersons is null)
        return connectedPersons; 
    }
}

代码:

@RelationshipEntity(type = "CONNECTED_TO") 
public class ConnectedPersons{ 

@GraphId private Long id; 

@StartNode private Person startPerson; 

@EndNode private Person endPerson; 

private String connectionProperty; 

public ConnectedPersons() { } 

public ConnectedPersons(Person startPerson, Person endPerson, String connectionProperty) {             this.startPerson = startPerson; this.endPerson = endPerson; this.connectionProperty = connectionProperty; 
}

我正在尝试与同一类建立关系..即连接到 Person 的人..当我调用 Junit 测试时:

    Person one = new Person ("One"); 

Person two = new Person ("Two"); 

personService.save(one); //Works also when I use template.save(one)

personService.save(two); 

Iterable<Person> persons = personService.findAll(); 

for (Person person: persons) { 
System.out.println("Person Name : "+person.getPersonName()); 
} 

one.connectsTo(two, "Sample Connection"); 

template.save(one);

当我尝试执行 one.connectsTo(two, "Prop"); 时,我得到空指针 请问你能告诉我哪里错了吗?

提前致谢。

【问题讨论】:

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


    【解决方案1】:

    除了缺少对 Set 的初始化之外,另一件事是 ConnectedPersons 类是一个@RelationshipEntity。但是在您的类 Person 中,您将它与 @RelatedTo 注释一起使用,就好像它是 @NodeEntity 一样。您应该在 Person 类中使用 @RelatedToVia 注解。

    【讨论】:

    • 是的,谢谢.. 我也仔细阅读并找到了答案。 :)
    【解决方案2】:

    您在以下代码中遇到空指针异常,因为您尚未初始化 connectedPersons 集合。

    this.connectedPersons.add(connectedPersons); //Null Pointer Here(connectedPersons is null)
    

    如下所示初始化集合

    @Fetch @RelatedTo(type = "CONNECTS_TO", direction = Direction.BOTH) 
    private Set<ConnectedPersons> connectedPersons=new HashSet<ConnectedPersons>();
    

    【讨论】:

    • 谢谢,但我确实初始化了 connectedPersons。我只是在这里把事情复杂化了。尽管如此,它已经解决了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多