Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jHipster 8.1.0 JDL generated @OneToOne and @ManyToOne relationship was annotated with FetchType.LAZY, getting the attribute of the entity causes LazyInitializationException No Session #25241

Closed
1 task done
kevintanhongann opened this issue Feb 18, 2024 · 3 comments

Comments

@kevintanhongann
Copy link

kevintanhongann commented Feb 18, 2024

Overview of the issue

JDL generated @OnetoOne relationship was annotated with FetchType.LAZY, getting the attribute of the entity causes LazyInitializationException. Previously it wasn't like that in jHipster 7.9.+

Reproduce the error
application {
  config {
    baseName brokenDemo
    applicationType monolith
    authenticationType jwt
    databaseType sql
    devDatabaseType h2Disk
    prodDatabaseType postgresql
    clientFramework vue
    buildTool gradle
    testFrameworks [gatling,cypress]
  }
  entities *
}

entity Company {
  name String required
  slug String required
  description String required
  logoUrl String
  createdDate Instant required
  createdBy String
  lastModifiedDate Instant
  lastModifiedBy String
}

entity UserCompany {
  createdDate Instant required
  createdBy String
  lastModifiedDate Instant
  lastModifiedBy String
}


relationship OneToOne {
    UserCompany{user(login)} to User with builtInEntity
}


relationship ManyToOne {
    UserCompany{company(name)} to Company
}
Suggest a Fix
JHipster Version(s)

8.1.0

Previously @OnetoOne relationships were annotated as per normal in jHipster 7.9.3+ as far as I remember.

JHipster configuration
.yo-rc.json file
{
  "generator-jhipster": {
    "applicationIndex": 0,
    "applicationType": "monolith",
    "authenticationType": "jwt",
    "baseName": "brokenDemo",
    "buildTool": "gradle",
    "clientFramework": "vue",
    "databaseType": "sql",
    "devDatabaseType": "h2Disk",
    "devServerPort": 9060,
    "entities": [
      "Company",
      "UserCompany"
    ],
    "jhipsterVersion": "8.1.0",
    "languages": [
      "en"
    ],
    "lastLiquibaseTimestamp": 1708265818000,
    "nativeLanguage": "en",
    "prodDatabaseType": "postgresql",
    "testFrameworks": [
      "gatling",
      "cypress"
    ]
  }
}
Environment and Tools

openjdk version "21.0.2" 2024-01-16 LTS
OpenJDK Runtime Environment Temurin-21.0.2+13 (build 21.0.2+13-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.2+13 (build 21.0.2+13-LTS, mixed mode, sharing)

git version 2.40.1

node: v18.19.0
npm: 10.2.3

Docker version 25.0.3, build 4debf41

JDL for the Entity configuration(s) entityName.json files generated in the .jhipster directory
JDL entity definitions
@ChangelogDate("20240218141558")
entity Company {
  name String required
  slug String required
  description String required
  logoUrl String
  createdDate Instant required
  createdBy String
  lastModifiedDate Instant
  lastModifiedBy String
}
@ChangelogDate("20240218141658")
entity UserCompany {
  createdDate Instant required
  createdBy String
  lastModifiedDate Instant
  lastModifiedBy String
}
relationship OneToOne {
  UserCompany{user(login)} to User with builtInEntity
}
relationship ManyToOne {
  UserCompany{company(name)} to Company
}

search Company, UserCompany with no

Entity configuration(s) entityName.json files generated in the .jhipster directory
<details>
<summary>.yo-rc.json file</summary>
<pre>
{
  "generator-jhipster": {
    "applicationIndex": 0,
    "applicationType": "monolith",
    "authenticationType": "jwt",
    "baseName": "brokenDemo",
    "buildTool": "gradle",
    "clientFramework": "vue",
    "databaseType": "sql",
    "devDatabaseType": "h2Disk",
    "devServerPort": 9060,
    "entities": [
      "Company",
      "UserCompany"
    ],
    "jhipsterVersion": "8.1.0",
    "languages": [
      "en"
    ],
    "lastLiquibaseTimestamp": 1708265818000,
    "nativeLanguage": "en",
    "prodDatabaseType": "postgresql",
    "testFrameworks": [
      "gatling",
      "cypress"
    ]
  }
}
</pre>
</details>


##### **Environment and Tools**

openjdk version "21.0.2" 2024-01-16 LTS
OpenJDK Runtime Environment Temurin-21.0.2+13 (build 21.0.2+13-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.2+13 (build 21.0.2+13-LTS, mixed mode, sharing)

git version 2.40.1

node: v18.19.0
npm: 10.2.3

Docker version 25.0.3, build 4debf41

##### **JDL for the Entity configuration(s) `entityName.json` files generated in the `.jhipster` directory**

<details>
<summary>JDL entity definitions</summary>

<pre>
@ChangelogDate("20240218141558")
entity Company {
  name String required
  slug String required
  description String required
  logoUrl String
  createdDate Instant required
  createdBy String
  lastModifiedDate Instant
  lastModifiedBy String
}
@ChangelogDate("20240218141658")
entity UserCompany {
  createdDate Instant required
  createdBy String
  lastModifiedDate Instant
  lastModifiedBy String
}
relationship OneToOne {
  UserCompany{user(login)} to User with builtInEntity
}
relationship ManyToOne {
  UserCompany{company(name)} to Company
}

search Company, UserCompany with no

</pre>
</details>
Browsers and Operating System

Ubuntu 23.10 and Firefox

  • Checking this box is mandatory (this is just to show you read everything)
@kevintanhongann kevintanhongann changed the title JDL generated @OneToOne relationship was annotated with FetchType.LAZY, getting the attribute of the entity causes LazyInitializationException No Session jHipster 8.1.0 JDL generated @OneToOne relationship was annotated with FetchType.LAZY, getting the attribute of the entity causes LazyInitializationException No Session Feb 18, 2024
@kevintanhongann
Copy link
Author

kevintanhongann commented Feb 18, 2024

GGoB5pSa8AAruwD

make it make sense...

Suggested fix

dont annotate OneToOne and ManyToOne fetch = FetchType.LAZY. Use the normal one like before.

Copy link
Contributor

github-actions bot commented Feb 18, 2024

JHipster has completed the sample check
.yo-rc.json: valid
Entities JDL: valid
Application: successfully generated
Frontend check: success
Backend check: success
E2E check: success

This check uses jhipster info output from the issue description to generate the sample.
Bug report that does not contain this information will be marked as invalid.

@kevintanhongann kevintanhongann changed the title jHipster 8.1.0 JDL generated @OneToOne relationship was annotated with FetchType.LAZY, getting the attribute of the entity causes LazyInitializationException No Session jHipster 8.1.0 JDL generated @OneToOne and @ManyToOne relationship was annotated with FetchType.LAZY, getting the attribute of the entity causes LazyInitializationException No Session Feb 19, 2024
@mshima
Copy link
Member

mshima commented Feb 19, 2024

  • hibernate 6 uses lazy fetch by default now.
  • eager fetch at entity level can fetch much more data than required.
  • we use *WithEagerRelationships apis for eager fetching. should be enabled by default.

CI is passing with provided sample

@mshima mshima closed this as completed Feb 19, 2024
@deepu105 deepu105 added this to the 8.2.0 milestone Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants