JHipster 8 beta JDL How to add relation to built in entity User

609 Views Asked by At

I am trying to associate a post to the currently logged in user but I get errors when I try to generate entities. What I'm trying to do seems similar to examples I see online and the documentation.

https://www.jhipster.tech/user-entity/

I'm using JHipster 8 installed from npm npm install -g generator-jhipster

I tried to generate entities with the following jdl file:

entity Post {
    content String,
}

/**
 * One to many relationship.
 */
relationship OneToMany {
    Post{user} to User
}

and got the following error:

ERROR! In the relationship between Post and User, User is not declared. If 'User' is a built-in entity declare like 'Post to User with builtInEntity'.

Following the advice from the error message I tried adding "with builtInEntity"

relationship OneToMany {
    Post{user} to User with builtInEntity
}

Then I got the following error

ERROR! Error at entity Post: could not find the other side of the relationship {
    "relationshipSide": "left",
    "relationshipType": "one-to-many",
    "otherEntityName": "user",
    "relationshipName": "user",
    "relationshipWithBuiltInEntity": true,
    "otherEntity": "[User Entity]",
    "ownerSide": false,
    "otherEntityField": "id",
    "relationshipLeftSide": true,
    "relationshipRightSide": false,
    "collection": true,
    "otherSideReferenceExists": false,
    "otherEntityIsEmbedded": false
}
1

There are 1 best solutions below

0
João Gonçalves On

The problem here is that you are specifying the binding. I had the same issue, and fixed it by removing the binding, in your case, the result should look like this:

relationship OneToMany {
    Post to User with builtInEntity
}

With this, it will create a user_id on the entity,
Notice: "Post" instead of "Post{user}"