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

Bidirectional One-To-Many relationships not generating #19780

Closed RamiSouai closed 11 months ago

RamiSouai commented 2 years ago
Overview of the issue

Hello, I have a JDL file containing a One-To-Many relationship (Using this basic example: https://www.jhipster.tech/jdl/relationships#basic-example) between two Entites Foo and Bar. You can find the JDL file below.

The resulting interfaces are as follows:

export interface IBar { id: number; foo?: Pick<IFoo, 'id'> | null; }

export interface IFoo { id: number; }

The Foo interface is missing the bar?: IBar[] property.

Reproduce the error

Create a 1: or :1 relationship in a JDL file then run jhipster jdl ./jdl-file.jdl

JHipster Version(s)

JHipster v7.9.3

JHipster configuration
.yo-rc.json file
{
  "generator-jhipster": {
    "applicationType": "monolith",
    "authenticationType": "oauth2",
    "baseName": "test",
    "blueprints": [],
    "buildTool": "gradle",
    "cacheProvider": "ehcache",
    "clientFramework": "angularX",
    "clientPackageManager": "npm",
    "clientTheme": "none",
    "clientThemeVariant": "",
    "creationTimestamp": 1663589307095,
    "databaseType": "sql",
    "devDatabaseType": "h2Memory",
    "devServerPort": 4200,
    "dtoSuffix": "DTO",
    "enableGradleEnterprise": false,
    "enableHibernateCache": true,
    "enableSwaggerCodegen": false,
    "enableTranslation": true,
    "entities": ["Foo", "Bar"],
    "entitySuffix": "",
    "jhiPrefix": "jhi",
    "jhipsterVersion": "7.9.3",
    "languages": ["en"],
    "lastLiquibaseTimestamp": 1663590192000,
    "messageBroker": false,
    "microfrontend": false,
    "microfrontends": [],
    "nativeLanguage": "en",
    "otherModules": [],
    "packageName": "com.mycompany.myapp",
    "pages": [],
    "prodDatabaseType": "postgresql",
    "reactive": false,
    "searchEngine": false,
    "serverPort": "8080",
    "serverSideOptions": [],
    "serviceDiscoveryType": "no",
    "skipCheckLengthOfIdentifier": false,
    "skipClient": false,
    "skipFakeData": false,
    "skipUserManagement": true,
    "testFrameworks": [],
    "websocket": false,
    "withAdminUi": true
  }
}

JDL entity definitions
entity Foo
entity Bar
relationship OneToMany {
  Foo{bar} to Bar{foo}
}

Environment and Tools

openjdk version "17.0.3" 2022-04-19 OpenJDK Runtime Environment Temurin-17.0.3+7 (build 17.0.3+7) OpenJDK 64-Bit Server VM Temurin-17.0.3+7 (build 17.0.3+7, mixed mode, sharing)

git version 2.25.1

node: v16.15.0

npm: 8.5.5

Docker version 20.10.17, build 100c701

Docker Compose version v2.10.2

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

{
  "applications": "*",
  "changelogDate": "20220919122311",
  "dto": "no",
  "embedded": false,
  "entityTableName": "foo",
  "fields": [],
  "fluentMethods": true,
  "jpaMetamodelFiltering": false,
  "name": "Foo",
  "pagination": "no",
  "readOnly": false,
  "relationships": [
    {
      "otherEntityName": "bar",
      "otherEntityRelationshipName": "foo",
      "relationshipName": "bar",
      "relationshipType": "one-to-many"
    }
  ],
  "service": "no"
}
Bar.json

{
  "applications": "*",
  "changelogDate": "20220919122312",
  "dto": "no",
  "embedded": false,
  "entityTableName": "bar",
  "fields": [],
  "fluentMethods": true,
  "jpaMetamodelFiltering": false,
  "name": "Bar",
  "pagination": "no",
  "readOnly": false,
  "relationships": [
    {
      "otherEntityName": "foo",
      "otherEntityRelationshipName": "bar",
      "relationshipName": "foo",
      "relationshipType": "many-to-one"
    }
  ],
  "service": "no"
}
Browsers and Operating System
RamiSouai commented 1 year ago

This is still an issue as of v7.9.3

I should also add that this only occurs when using Angular. React and Vue generate the models correctly.

dhardjono commented 1 year ago

Can confirm. Any fix planned?

Tcharl commented 1 year ago

Hi,

Expected since the very beginning of the project due to N+1 fetch and performance. There's no UI component that displays the list thus not needed. Feel free to contribute, or at least to find an elegant solution that can remedy this issue to go further

RamiSouai commented 1 year ago

Hi,

Expected since the very beginning of the project due to N+1 fetch and performance. There's no UI component that displays the list thus not needed. Feel free to contribute, or at least to find an elegant solution that can remedy this issue to go further

Thank you for your answer. Maybe we were doing something wrong in the project, but this use case was working until version 7.9.2. I tried displaying the list in a UI component and the property is still not generated. Am I missing something?

mshima commented 1 year ago

Reposting https://github.com/jhipster/generator-jhipster/pull/21117#issuecomment-1517828616:

The ownerSide is a sql term that is used to build the rest api.
IMO it should be removed from jdl and replaced with something else in every generator except sql.

ownerSide is only required for bidirectional OneToOne relationships. OneToMany and ManyToOne currently implies ownerSide on the many side, but could be avoided by using a join table. ManyToMany implies ownerSide on both sides.

If we want the ownerSide on the OneToMany, we should try a join table approach.

Tcharl commented 1 year ago

For regular updates/resync with jhipster platform releases, please follow that guildeline

Weltraumschaf commented 11 months ago

Hi @mshima 👋

We investedigated the issue with support of @emilpaw.

TL;DR: You fixed in v7.9.3 a very old bug we accidentally rely on in our code base 🙈

The missing properties were never generated in the whole Java code, but in the frontend generated code and we use these properties. You fixed this in the Angular generator, when you upgraded the Angular version. While debuging/experimenting we found out that the Vue generator works for us and we tried to fix the Angular generator the same way. That lead to our wrong PR. Now we know that our attempt to "fix the problem" would reintroduce the bug you've fixed 😂 We think that maybe the Vue generator still has the bug and generates these properties as empty arrays. @emilpaw will file an issue because he can explain it way better than me.

So, you can close this issue 😬

@RamiSouai will close the PR: https://github.com/jhipster/generator-jhipster/pull/21117

DanielFran commented 11 months ago

Thanks @Weltraumschaf for the info

mshima commented 11 months ago

@Weltraumschaf thanks for the follow up. We can handle entity improvements during v8 cycle.