Satish
Satish

Reputation: 6485

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

Want to create entity called Friendship and want to leverage the User entity that's part of Jhipster, But I keep getting this invalid-relationship error (full error below).

User has friends (user entities) and vice-versa


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:

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.

Upvotes: 1

Views: 2244

Answers (2)

vicpermir
vicpermir

Reputation: 3702

You have several problems in your JDL. For example, you should not mix relationships and entities like this:

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

If I have understood your requirements correctly you want to design a kind of blog and let users form friendships. JDL will not let you add relationships starting from the core entity User so you have created a UserExtended and will possibly store some extra information there.

Remember that you can design multiple relationships inside one relationship block. In fact I think it is a good practice, makes the whole JDL a bit more readable.

This should do what you need:

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
}

The only tricky part here is the many-to-many between two users in a relationship called Friend. You need to store some extra information about the friendship (status, modified, created) so we had to split this many-to-many into a one-to-many plus a many-to-one using the Friend entity as a join table with extra fields.

I did not change your naming scheme, which could probably be improved.

Remember to check the official documentation and optionally use JHipster Online for JDL storage and validation.

Upvotes: 3

Alejandro
Alejandro

Reputation: 826

Try renaming

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

to

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

Upvotes: 0

Related Questions