Spring Data Neo4j @RelationshipEntity子类?

时间:2016-08-19 19:57:13

标签: neo4j spring-data-neo4j-4 neo4j-ogm

我正在为我的第一个Spring Data Neo4j应用程序建模,并且想知道关于@RelationshipEntity类的子类化 - 1)它可以完成,2)这是一个好主意吗?

以下是我正在考虑使用RSS的一个例子。

Feed有很多Entry个,有3种类型的条目:

  1. 原始条目(新内容)
  2. 重新发布内容
  3. 喜欢内容(实际上是退化的Reblog)
  4. Feed可能如下所示: @Relationship List<Entry> entries; 其中Liked是Reblog的子类,它是Entry的子类。

    这似乎更自然,因为RelationshipEntities是第一类对象: @Relationship(type="Content", Relationship.OUTGOING) List<Entry> entries; ... @RelationshipEntity(type="Content") public class Content { ... @RelationshipEntity(type="RebloggedContent") public class RebloggedContent extends Content { ... @RelationshipEntity(type="LikedContent") public class LikedContent extends Content { ... 正如我所说,这是我的第一个Neo4j应用程序,所以我不知道这些想法是否有任何好处。

    从查询的角度来看,我想要提出有关EntryEntry整体的特定类型(或类型组合)的问题。

    赞赏设计/建模创意的指示。

1 个答案:

答案 0 :(得分:2)

可以使用以下警告对子关系实体进行子类化:

  • 每个子类关系实体必须声明一个额外的区别属性,使其与基类区分开来 - 这个信息被OGM工具用于类型内省。

示例:

以下是基本关系实体的示例(使用Kotlin JVM语言):

abstract class Relationship
{
    @GraphId
    internal var graphId: Long?
        private set

    @StartNode
    var auditioner: CandidateProfile

    @EndNode
    var auditionee: CandidateProfile

    var createdDate: Date

    init
    {
        this.graphId = null
        this.auditioner = CandidateProfile()
        this.auditionee = CandidateProfile()
        this.createdDate = Date()
    }

    abstract fun mutualRelationship(): Relationship?


}

与子类一起:

@RelationshipEntity(type = "MAYBE_LATER")
class MaybeLater constructor(auditioner: CandidateProfile,
                             auditionee: CandidateProfile,
                             timeOut: Date?) : Relationship()
{
    var timeOut: Date?
    var count: Int

    init
    {
        this.auditioner = auditioner
        this.auditionee = auditionee
        this.timeOut = timeOut
        this.count = 1
    }

    //Provide default constructor for OGM
    constructor() : this(CandidateProfile(), CandidateProfile(), null)


    override fun mutualRelationship(): MaybeLater?
    {
        return auditionee.maybeLaters.find { it.auditionee == auditioner }
    }
}

<强>用法:

class CandidateProfile {

    @Relationship(type = "LIKES", direction = Relationship.OUTGOING)
    var likes: MutableSet<Like>

    @Relationship(type = "DISLIKES", direction = Relationship.OUTGOING)
    var dislikes: MutableSet<Dislike>

    @Relationship(type = "MAYBE_LATER", direction = Relationship.OUTGOING)
    var maybeLaters: MutableSet<MaybeLater>
}

请注意,我们为每个单独的关系类型定义一个集合。如果需要单个聚合集合,则需要在代码中完成。

Neo4j用户松弛频道:

除了StackOverflow,Neo4j社区还通过Neo4j Users public Slack channel提供支持 - 非常鼓励加入。