我正在为我的第一个Spring Data Neo4j应用程序建模,并且想知道关于@RelationshipEntity类的子类化 - 1)它可以完成,2)这是一个好主意吗?
以下是我正在考虑使用RSS的一个例子。
Feed
有很多Entry
个,有3种类型的条目:
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应用程序,所以我不知道这些想法是否有任何好处。
从查询的角度来看,我想要提出有关Entry
和Entry
整体的特定类型(或类型组合)的问题。
赞赏设计/建模创意的指示。
答案 0 :(得分:2)
可以使用以下警告对子关系实体进行子类化:
示例:强>
以下是基本关系实体的示例(使用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提供支持 - 非常鼓励加入。