DM 中的 Neo4j 递归

Neo4j Recursivenes in DM

我最近一直在为一个 NeoUser 可以拥有多个 NeoPosts 的项目开发我的域模型。每个 Neopost 都使用名为 user 的字段引用其所有者。 现在我注意到,当我将 Neopost 设置为 NeoUser.thisusersposts 中的一个条目以及 NeoImage 对象中的用户时,我得到了一个递归关系和一个 Whosebug 错误。

我尝试实现的模型应该如下所示:

NeoPost:

@Id
@GeneratedValue
Long postId;

@NotNull
@NotBlank
@Size(min = 1, max = 600)
String question;

/**
 * Images that are involved in that post
 */
@NotEmpty
@Relationship(type = "STARES", direction = Relationship.OUTGOING)
Set<Neoimage> neoimageSet = new HashSet<>();

/**
 * User that made this post
 */
@Relationship(type = "WAS_CREATED_BY", direction = Relationship.OUTGOING)
NeoUser user;


/**
 * Users that somehow in a way possible described in Userinteractiontype enum
 * with this current post.
 */
@Relationship(type = "INTERACTED_WITH", direction = Relationship.INCOMING)
Set<NeoInteraction> incomingInteractions = new HashSet<>();

新用户:

 @Id
@GeneratedValue
Long nodeId;

@Size(min = 2, max = 20, message = "Username length has to be between 2 and 20 characters.")
@Property(name = "user")
String username;

//Relationships / Interactions that were initiated by this user with image of another user
@Relationship(type = "INTERACTED_WITH", direction = Relationship.OUTGOING)
Set<NeoInteraction> outgoingInteraction = new HashSet<>();

//Posts that this user created
@Relationship(type = "OWNS", direction = Relationship.OUTGOING)
Set<NeoPost> thisusersposts = new HashSet<>();

@Relationship(type = "SUBSCRIBED", direction = Relationship.OUTGOING)
Set<NeoUser> usersubscriptions = new HashSet<>();

我现在想问的是:

是否有一些注释可以防止这种递归性,从而防止 Whosebug 错误?非常感谢。

好吧,我找到了解决问题的方法。 经过几个小时的调试,我通过使用 Lombok 注释排除 NeoPost 中的用户来消除哈希码中的循环引用:

@EqualsAndHashCode(exclude = "user")