【问题标题】:Spring Data Neo4j @RelationshipEntity subclasses?Spring Data Neo4j @RelationshipEntity 子类?
【发布时间】:2016-08-19 19:57:13
【问题描述】:

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

这是我正在考虑使用 RSS 的一个示例。

一个Feed有很多Entrys,有3种入口:

  1. 原始条目(新内容)
  2. 转发的内容
  3. 喜欢的内容(实际上是退化的 Reblog)

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 应用程序,所以我不知道这些想法是否有任何好处。

从查询的角度来看,我想就EntryEntrys 的特定类型(或类型组合)作为一个整体提出问题。

感谢您提供设计/建模想法的指针。

【问题讨论】:

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


    【解决方案1】:

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

    • 每个子类关系实体必须声明一个额外的区别属性,将其与基类区分开来 - 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 用户 Slack 频道:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多