Jhipster-为用户实体建立多对多关系

时间:2020-02-03 21:04:58

标签: jhipster jdl

想要创建一个名为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.

2 个答案:

答案 0 :(得分: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-many实体将此one-to-many分成many-to-one加上Friend作为具有额外字段的联接表。

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

请记住检查official documentation,并选择使用JHipster Online进行JDL存储和验证。

答案 1 :(得分:0)

尝试重命名

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

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