jhipster / generator-jhipster

JHipster is a development platform to quickly generate, develop, & deploy modern web applications & microservice architectures.
https://www.jhipster.tech
Apache License 2.0
21.5k stars 4.02k forks source link

Relation to User entity does not work on JHipster 8 beta #22844

Closed damy90 closed 11 months ago

damy90 commented 1 year ago
Overview of the issue

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.

Without the association entities will be added successfully and administration pages will be added to the app

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

Example jdl file

entity Post {
    content String,
}

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

Error:

INFO! Generating jdls jhipster-jdl.jdl
ERROR! An error occured while running jhipster:jdl#parseJDL
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'.
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'.
    at checkForAbsentEntities (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/jdl/validators/jdl-without-application-validator.js:170:15)
    at file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/jdl/validators/jdl-without-application-validator.js:114:13
    at file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/jdl/models/jdl-object.js:183:13
    at file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/jdl/models/jdl-relationships.js:78:13
    at Array.forEach (<anonymous>)
    at JDLRelationships.forEach (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/jdl/models/jdl-relationships.js:77:24)
    at JDLObject.forEachRelationship (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/jdl/models/jdl-object.js:182:28)
    at checkForRelationshipErrors (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/jdl/validators/jdl-without-application-validator.js:112:19)
    at Object.checkForErrors (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/jdl/validators/jdl-without-application-validator.js:56:13)
    at checkForErrors (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/jdl/jdl-importer.js:169:15)
ERROR! An error occured while running jhipster:jdl#customizeApplication
ERROR! An error occured while running jhipster:jdl#configure
ERROR! An error occured while running jhipster:jdl#generateJson
ERROR! An error occured while running jhipster:jdl#generate

Following the advice from the error message I tried adding "with builtInEntity". Note, it is not mentioned in the documentation I found.

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

New error when re-running jhipster to update the admin panel: ERROR! An error occured while running jhipster:bootstrap-application-base#prepareRelationshipsForTemplates ERROR! ERROR! Error at entity Post: could not find the other side of the relationship { "otherEntityName": "user", "relationshipName": "user", "relationshipSide": "left", "relationshipType": "one-to-many", "relationshipWithBuiltInEntity": true, "otherEntity": "[User Entity]", "ownerSide": false, "otherEntityField": "id", "relationshipLeftSide": true, "relationshipRightSide": false, "collection": true, "otherSideReferenceExists": false, "otherEntityIsEmbedded": false } Error: Error at entity Post: could not find the other side of the relationship { "otherEntityName": "user", "relationshipName": "user", "relationshipSide": "left", "relationshipType": "one-to-many", "relationshipWithBuiltInEntity": true, "otherEntity": "[User Entity]", "ownerSide": false, "otherEntityField": "id", "relationshipLeftSide": true, "relationshipRightSide": false, "collection": true, "otherSideReferenceExists": false, "otherEntityIsEmbedded": false } at prepareRelationship (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/generators/base-application/support/prepare-relationship.mjs:83:19) at BootstrapApplicationBase.prepareRelationshipsForTemplates (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/generators/bootstrap-application-base/generator.mjs:210:17) at BootstrapApplicationBase.executeTask (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/node_modules/yeoman-generator/dist/actions/lifecycle.js:240:26) at env.queueTask.once (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/node_modules/yeoman-generator/dist/actions/lifecycle.js:214:56) at runLoop.add.once (C:\Users\daniel.popov\AppData\Roaming\npm\node_modules\generator-jhipster\node_modules\yeoman-environment\lib\environment.js:1441:34) at Immediate. (C:\Users\daniel.popov\AppData\Roaming\npm\node_modules\generator-jhipster\node_modules\grouped-queue\lib\subqueue.js:48:34) at process.processImmediate (node:internal/timers:476:21) ERROR! An error occured while running jhipster:bootstrap-application#prepareEntityDerivedProperties ERROR! An error occured while running jhipster:bootstrap-application#postPreparingEntities info KeyStore 'C:\dany\test\user-association-jhipster\src\main\resources\config\tls\keystore.p12' already exists. Leaving unchanged. ERROR! An error occured while running jhipster:server#writeServerFiles ERROR! An error occured while running jhipster:angular#writeEntitiesFiles info Using existing git repository. ERROR! An error occured while running jhipster:spring-data-relational#addTestSpringFactory

Motivation for or Use Case

I am trying to associate a post to the currently logged in user and generate the administration page for the app

Reproduce the error
  1. Run jhipster

  2. Add and execute jdl file: jhipster jdl jhipster-jdl.jdl

    
    entity Post {
    content String,
    }

/**

  1. Add "with builtInEntity"
    relationship OneToMany {
    Post{user} to User with builtInEntity
    }
  2. Update app: jhipster

The 2nd error should appear

JHipster Version(s)

Using JHipster 8 beta, the default installed by npm

I tried downgrading to 7.9.3 but the jhipster command wouldn't work at all after that

JHipster configuration

WARNING! Since JHipster v8, the jhipster command will not use the locally installed generator-jhipster. If you want to execute the locally installed generator-jhipster, run: npx jhipster

    ██╗ ██╗   ██╗ ████████╗ ███████╗   ██████╗ ████████╗ ████████╗ ███████╗
    ██║ ██║   ██║ ╚══██╔══╝ ██╔═══██╗ ██╔════╝ ╚══██╔══╝ ██╔═════╝ ██╔═══██╗
    ██║ ████████║    ██║    ███████╔╝ ╚█████╗     ██║    ██████╗   ███████╔╝

██╗ ██║ ██╔═══██║ ██║ ██╔════╝ ╚═══██╗ ██║ ██╔═══╝ ██╔══██║ ╚██████╔╝ ██║ ██║ ████████╗ ██║ ██████╔╝ ██║ ████████╗ ██║ ╚██╗ ╚═════╝ ╚═╝ ╚═╝ ╚═══════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══════╝ ╚═╝ ╚═╝ https://www.jhipster.tech Welcome to JHipster v8.0.0-beta.1

Welcome to the JHipster Information Sub-Generator

user-association-jhipster@0.0.1-SNAPSHOT C:\dany\test\user-association-jhipster
`-- generator-jhipster@8.0.0-beta.1
JHipster configuration, a .yo-rc.json file generated in the root folder
.yo-rc.json file
{
  "applicationType": "monolith",
  "authenticationType": "jwt",
  "baseName": "user-association-jhipster",
  "buildTool": "maven",
  "cacheProvider": "ehcache",
  "clientFramework": "angular",
  "clientTheme": "none",
  "creationTimestamp": 1689164822432,
  "databaseType": "sql",
  "devDatabaseType": "h2Disk",
  "devServerPort": 4200,
  "dtoSuffix": "DTO",
  "enableGradleEnterprise": null,
  "enableHibernateCache": true,
  "enableSwaggerCodegen": false,
  "enableTranslation": true,
  "entities": [
    "Post"
  ],
  "entitySuffix": "",
  "gradleEnterpriseHost": null,
  "jhiPrefix": "jhi",
  "jhipsterVersion": "8.0.0-beta.1",
  "languages": [
    "en",
    "fr"
  ],
  "lastLiquibaseTimestamp": 1689165719000,
  "messageBroker": false,
  "microfrontend": false,
  "microfrontends": [],
  "nativeLanguage": "en",
  "packageFolder": "com/mycompany/myapp",
  "packageName": "com.mycompany.myapp",
  "pages": [],
  "prodDatabaseType": "postgresql",
  "reactive": false,
  "searchEngine": false,
  "serverPort": null,
  "serverSideOptions": [],
  "serviceDiscoveryType": false,
  "skipCheckLengthOfIdentifier": false,
  "skipClient": false,
  "skipFakeData": false,
  "skipUserManagement": false,
  "testFrameworks": [],
  "websocket": false,
  "withAdminUi": true
}
Environment and Tools

openjdk version "17.0.7" 2023-04-18 OpenJDK Runtime Environment Temurin-17.0.7+7 (build 17.0.7+7) OpenJDK 64-Bit Server VM Temurin-17.0.7+7 (build 17.0.7+7, mixed mode, sharing)

git version 2.40.1.windows.1

node: v18.15.0 npm: 9.5.0

'docker' command could not be found

JDL for the Entity configuration(s) entityName.json files generated in the .jhipster directory
JDL entity definitions
entity Post {
  content String
}
relationship OneToMany {
  Post{user} to User with builtInEntity
}

search Post with no

force .yo-rc.json

Congratulations, JHipster execution is complete!

Entity configuration(s) entityName.json files generated in the .jhipster directory

Post.json:

{
  "applications": "*",
  "changelogDate": "20230712124159",
  "entityTableName": "post",
  "fields": [
    {
      "fieldName": "content",
      "fieldType": "String"
    }
  ],
  "name": "Post",
  "relationships": [
    {
      "otherEntityName": "user",
      "relationshipName": "user",
      "relationshipSide": "left",
      "relationshipType": "one-to-many",
      "relationshipWithBuiltInEntity": true
    }
  ],
  "searchEngine": "no"
}

jhipster-jdl.jdl:

entity Post {
    content String,
}

/**
 * One to many relationship.
 */
relationship OneToMany {
    Post{user} to User with builtInEntity
}
Browsers and Operating System

Windows 10 Chrome

mraible commented 1 year ago

Change your JDL to the following, and it should work:

relationship OneToMany {
    Post{user} to User with builtInEntity
}
mshima commented 1 year ago

Many at builtin User side is not supported. The User side is immutable and join table is only supported by many-to-many relationships.

mshima commented 1 year ago

We should provide a better error message.

damy90 commented 1 year ago

@mraible Yes I followed the instructions in the error and got a different error. I still decided to include the code without " with builtInEntity" because it's closer to the documentation. So 1 to 1 relationship is not supported either? You have that in an example here: https://www.jhipster.tech/user-entity/ It does not compile successfully.

damy90 commented 1 year ago

I think I read somewhere on stackoverflow that one to many relationships FROM User do not work because it's immutable and you shouldn't be able to add a field to reference other entities but why wouldn't I be able to add a link in Posts? I would expect Many to Many to not work. I just tried with many to many.

entity Post {
    content String,
}

relationship ManyToMany {
    Post{user} to User{post} with builtInEntity
}

It compiled with an error, the site runs but it's broken

ERROR! Error editing file C:\dany\test\user-association-jhipster\src\test\resources\META-INF\spring.factories: Cannot read properties of null (reading 'match') at TypeError: Cannot read properties of null (reading 'match')
    at JHipsterServerGenerator.<anonymous> (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/generators/base/support/write-files.mjs:29:32)
    at writeCallback (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/generators/base/generator-base.mjs:411:58)
    at JHipsterServerGenerator.editFile (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/generators/base/generator-base.mjs:425:16)
    at source.addTestSpringFactory (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/generators/server/generator.mjs:291:26)
    at SqlGenerator.addTestSpringFactory (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/generators/spring-data-relational/generator.mjs:75:46)
    at SqlGenerator.executeTask (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/node_modules/yeoman-generator/dist/actions/lifecycle.js:240:26)
    at env.queueTask.once (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/node_modules/yeoman-generator/dist/actions/lifecycle.js:214:56)
    at runLoop.add.once (C:\Users\daniel.popov\AppData\Roaming\npm\node_modules\generator-jhipster\node_modules\yeoman-environment\lib\environment.js:1441:34)
    at Immediate._onImmediate (C:\Users\daniel.popov\AppData\Roaming\npm\node_modules\generator-jhipster\node_modules\grouped-queue\lib\subqueue.js:48:34)
    at process.processImmediate (node:internal/timers:476:21)
Error: Error editing file C:\dany\test\user-association-jhipster\src\test\resources\META-INF\spring.factories: Cannot read properties of null (reading 'match') at TypeError: Cannot read properties of null (reading 'match')
    at JHipsterServerGenerator.<anonymous> (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/generators/base/support/write-files.mjs:29:32)
    at writeCallback (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/generators/base/generator-base.mjs:411:58)
    at JHipsterServerGenerator.editFile (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/generators/base/generator-base.mjs:425:16)
    at source.addTestSpringFactory (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/generators/server/generator.mjs:291:26)
    at SqlGenerator.addTestSpringFactory (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/generators/spring-data-relational/generator.mjs:75:46)
    at SqlGenerator.executeTask (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/node_modules/yeoman-generator/dist/actions/lifecycle.js:240:26)
    at env.queueTask.once (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/node_modules/yeoman-generator/dist/actions/lifecycle.js:214:56)
    at runLoop.add.once (C:\Users\daniel.popov\AppData\Roaming\npm\node_modules\generator-jhipster\node_modules\yeoman-environment\lib\environment.js:1441:34)
    at Immediate._onImmediate (C:\Users\daniel.popov\AppData\Roaming\npm\node_modules\generator-jhipster\node_modules\grouped-queue\lib\subqueue.js:48:34)
    at process.processImmediate (node:internal/timers:476:21)
    at writeCallback (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/generators/base/generator-base.mjs:419:27)
    at JHipsterServerGenerator.editFile (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/generators/base/generator-base.mjs:425:16)
    at source.addTestSpringFactory (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/generators/server/generator.mjs:291:26)
    at SqlGenerator.addTestSpringFactory (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/dist/generators/spring-data-relational/generator.mjs:75:46)
    at SqlGenerator.executeTask (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/node_modules/yeoman-generator/dist/actions/lifecycle.js:240:26)
    at env.queueTask.once (file:///C:/Users/daniel.popov/AppData/Roaming/npm/node_modules/generator-jhipster/node_modules/yeoman-generator/dist/actions/lifecycle.js:214:56)
    at runLoop.add.once (C:\Users\daniel.popov\AppData\Roaming\npm\node_modules\generator-jhipster\node_modules\yeoman-environment\lib\environment.js:1441:34)
    at Immediate.<anonymous> (C:\Users\daniel.popov\AppData\Roaming\npm\node_modules\generator-jhipster\node_modules\grouped-queue\lib\subqueue.js:48:34)
    at process.processImmediate (node:internal/timers:476:21)

Translations are broken and I can't log in with the default test users "user" and "admin" image Edit: That error probably is not related Edit 2: Ignoring errors when the application works fine, it's probably an issue with the Angular template Edit 3: The many to many relation worked. Apparently trying to generate the project several times or from scratch even after deleting the project can cause problems. You have to change the project name to get around random errors. My list of "test-jshint-#" folders is growing. This seems finnicky sometimes.

cairiza commented 10 months ago

Change your JDL to the following, and it should work:

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

thanks!

AssaneT commented 9 months ago

Change your JDL to the following, and it should work:

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

Thanks so much !