关系@EndNode 的通用性

Genericity on Relationship @EndNode

我有一个名为 "AUTHORED" 的关系,此关系链接 UserTipComment,或 Build

为了避免为每个关系端节点类型创建一个 class,我决定编写一个通用的 Authored class:

@RelationshipEntity(type="AUTHORED")
public class Authored<T> {

    @GraphId Long id;

    @StartNode
    private User author;

    @EndNode
    private T entity;

    private Long date;

    public User getAuthor() {
        return author;
    }

    public T getEntity() {
        return entity;
    }

    public Long getDate() {
        return date;
    }

    public void setDate(Long date) {
        this.date = date;
    }
}

这是我的用户 class 的一部分:

@NodeEntity
public class User extends Entity{

    private String firstName;

    private String lastName;

    private String ign;

    @JsonIgnore
    private String password;

    private String email;

    @Relationship(type="AUTHORED", direction="OUTGOING")
    @JsonIgnore private Set<Authored<Tip>> tips = new HashSet<Authored<Tip>>();

    public Set<Tip> getTips() {
        Set<Tip> tips = new HashSet<Tip>();
        this.tips.forEach((t) -> {
            tips.add(t.getEntity());
        });
        return tips;
    }
...
...

这里的问题是我得到一个 RunTimeException:

java.lang.RuntimeException: Cannot find a writer for the EndNode of relational entity com.bnstips.api.database.model.relation.Authored
    at org.neo4j.ogm.context.GraphEntityMapper.createRelationshipEntity(GraphEntityMapper.java:340) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
    at org.neo4j.ogm.context.GraphEntityMapper.mapRelationshipEntity(GraphEntityMapper.java:280) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
    at org.neo4j.ogm.context.GraphEntityMapper.mapRelationships(GraphEntityMapper.java:243) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
    at org.neo4j.ogm.context.GraphEntityMapper.mapEntities(GraphEntityMapper.java:143) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
    at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:117) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
    at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:81) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
    at org.neo4j.ogm.session.delegates.LoadOneDelegate.load(LoadOneDelegate.java:48) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
    at org.neo4j.ogm.session.Neo4jSession.load(Neo4jSession.java:82) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
    at com.bnstips.api.database.GenericService.find(GenericService.java:22) ~[classes/:na]
    at com.bnstips.api.endpoint.UserEndpoint.getUserTips(UserEndpoint.java:33) ~[classes/:na]
...........
...........     

那么,真的可以创建通用关系吗?我做错了什么吗?

编辑:如果这不可能,我应该为每个创作的端节点类型创建一个 classe,还是应该创建 3 个关系,每个创作的端节点类型一个(AUTHORED_TIPAUTHORED_COMMENTAUTHORED_BUILD).

不支持通用 Authored。您的两种选择都可以。

每个端节点类型都可以有一个特定的关系类型,因此三个关系实体 类 用不同的关系类型注释(AUTHORED_TIPAUTHORED_COMMENTAUTHORED_BUILD ).

或者,如果您的模型认为更好,则使用相同的关系类型。您仍然需要三个关系实体 类,但使用相同的关系类型 (AUTHORED) 对它们进行注释。这方面的一个例子是 Pizza class that has references to two relationship entities represented by the same relationship HAS - PizzaSeasoning and PizzaCheese

请注意,对于以上两个选项,您的 start/end 节点是特定的并且不使用泛型。