【问题标题】:spring-data neo4j: can't find node entity by related entityspring-data neo4j:无法通过相关实体找到节点实体
【发布时间】:2012-12-25 13:36:12
【问题描述】:

使用 spring-data neo4j,我有 2 个节点实体:sn-p 和 person。每个 sn-p 都有一个作者。以下是类,然后是失败的测试:

@NodeEntity
public class SnippetLink {
    @GraphId
    @GeneratedValue
    Long id;

    @Fetch
    @RelatedTo(type = "AUTHORED")
    @Indexed
    PersonLink author;

    @RelatedToVia(type = SnippetRelation.DERIVED_FROM)
    SnippetLink parent;

    Long documentId;

    public SnippetLink() {
    }

    public SnippetLink(PersonLink author, Long documentId) {
        this.author = author;
        this.documentId = documentId;
    }

    public PersonLink getAuthor() {
        return author;
    }

    public void setAuthor(PersonLink author) {
        this.author = author;
    }

    public Long getDocumentId() {
        return documentId;
    }

    public void setDocumentId(Long documentId) {
        this.documentId = documentId;
    }

    public SnippetLink getParent() {
        return parent;
    }

    public void setParent(SnippetLink parent) {
        this.parent = parent;
    }

    public SnippetRelation fork(PersonLink author) {
        SnippetLink child = new SnippetLink(author, documentId);
        return new SnippetRelation(this, child, SnippetRelation.Type.Fork);
    }

    public SnippetRelation revision(Long documentId) {
        SnippetLink child = new SnippetLink(getAuthor(), documentId);
        return new SnippetRelation(this, child, SnippetRelation.Type.Revision);
    }
}

.

@NodeEntity
public class PersonLink {
    @GraphId
    Long id;

    String login;

    String firstName;

    String lastName;

    public PersonLink() {
    }

    public PersonLink(String login, String firstName, String lastName) {
        this.login = login;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

.

public interface SnippetLinkRepository extends GraphRepository<SnippetLink>{
    Iterable<SnippetLink> findByAuthor(PersonLink author);
}

.

PersonLink author = template.save(new PersonLink("johns", "John", "Smith"));

SnippetLink snippet = template.save(new SnippetLink(author, -1L));

SnippetRelation revisionRelation = snippet.revision(-2L);
template.save(revisionRelation.getChild());
template.save(revisionRelation);

Iterable<SnippetLink> snippets = snippetLinkRepository.findByAuthor(author);
assertThat(snippets).hasSize(2);

【问题讨论】:

  • 您的SnippetRelation 课程代码丢失。

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


【解决方案1】:
@RelatedToVia(type = SnippetRelation.DERIVED_FROM)
SnippetLink parent;

这应该是

@RelatedTo(type = SnippetRelation.DERIVED_FROM)
SnippetLink parent;

RelatedToVia 用于访问实体的关系(实体)。 然而,由于 SnippetLink 是一个实体,因此您需要使用 realatedTo。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 2013-03-06
    • 1970-01-01
    • 2015-04-19
    • 2020-04-12
    相关资源
    最近更新 更多