Jhipster - 为用户实体设置多对多关系

Jhipster - setting up many to many relationship for user-entities

想要创建名为 Friendship 的实体,并想利用属于 Jhipster 的用户实体, 但是我不断收到此无效关系错误(下面是完整错误)。

用户有朋友(用户实体),反之亦然


entity UserExtended {

}
entity Friend{
    status Boolean, 
    modified LocalDate,
    created LocalDate
}
relationship OneToOne {
    UserExtended{user(login)} to User
}

relationship OneToMany {
    UserExtended{friends} to Friend{user}
}

relationship ManyToOne {
    UserExtended{friend} to UserExtended{users}
}

entity Post {
    owner UserExtended,
    content String,
    dateCreated LocalDate 
}

entity Like {
    likedBy UserExtended,
    post Post,
    dateCreated LocalDate   
}

entity Comment {
    postedBy UserExtended,
    post Post,
    dateCreated LocalDate
}


relationship OneToMany {
  UserExtended{post} to Post{user}
}

relationship OneToMany {
  Like{post} to Post{like}
}

relationship OneToMany {
  Comment{post} to Post{comment}
}

错误:

Error: Can't add invalid relationship. Error: In the Many-to-One relationship from UserExtended to UserExtended, only unidirectionality is supported, you should either create a bidirectional One-to-Many relationship or remove the injected field in the destination entity instead.
Error while parsing applications and entities from the JDL Error: Can't add invalid relationship. Error: In the Many-to-One relationship from UserExtended to UserExtended, only unidirectionality is supported, you should either create a bidirectional One-to-Many relationship or remove the injected field in the destination entity instead.
Error: Can't add invalid relationship. Error: In the Many-to-One relationship from UserExtended to UserExtended, only unidirectionality is supported, you should either create a bidirectional One-to-Many relationship or remove the injected field in the destination entity instead.

尝试重命名

relationship OneToOne {
    UserExtended{user(login)} to User
}

relationship OneToOne {
    UserExtended{somethingElse(login)} to User
}

你的 JDL 有几个问题。例如,您不应该像这样混合关系和实体:

entity Post {
    owner UserExtended,   // <-- This is a problem
    content String,
    dateCreated LocalDate 
}

如果我没有正确理解你的需求,你想设计一种博客,让用户结交朋友。 JDL 不允许您添加从核心实体 User 开始的关系,因此您创建了一个 UserExtended 并且可能会在那里存储一些额外信息。

请记住,您可以在一个 relationship 块中设计多个关系。事实上,我认为这是一个很好的做法,使整个 JDL 更具可读性。

这应该可以满足您的需要:

entity UserExtended

entity Friend {
    status Boolean
    modified LocalDate
    created LocalDate
}

entity Post {
    content String
    dateCreated LocalDate
}

entity Like {
    dateCreated LocalDate
}

entity Comment {
    dateCreated LocalDate
}

relationship OneToOne {
    UserExtended{user(login)} to User
}

relationship ManyToOne {
    Post{owner} to UserExtended
    Comment{postedBy} to UserExtended
    Like{likedBy} to UserExtended
    Friend{user} to UserExtended
}

relationship OneToMany {
    UserExtended{friends} to Friend
    Post{likes} to Like
    Post{comments} to Comment
}

这里唯一棘手的部分是 many-to-many 两个用户之间的 Friend 关系。您需要存储一些关于友谊的额外信息(状态、修改、创建),因此我们必须使用 Friend 实体将此 many-to-many 拆分为 one-to-manymany-to-one作为具有额外字段的连接 table。

我没有更改您的命名方案,这可能会有所改进。

记得检查 official documentation and optionally use JHipster Online JDL 存储和验证。