1

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

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


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.
4

2 回答 2

2

您的 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. 您需要存储一些有关友谊的额外信息(状态、已修改、已创建),因此我们必须将其拆分many-to-manyone-to-many加号,many-to-oneFriend实体用作具有额外字段的连接表。

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

记得查看官方文档并选择使用JHipster Online进行 JDL 存储和验证。

于 2020-02-04T08:37:56.753 回答
0

尝试重命名

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

relationship OneToOne {
    UserExtended{somethingElse(login)} to User
}
于 2020-02-03T23:03:22.283 回答