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.4k stars 4.02k forks source link

Deleting a Parent isn't working when it has a Child in a one-to-one relationship #25797

Open vw98075 opened 4 months ago

vw98075 commented 4 months ago
Overview of the issue

With the following relationship definition,

@service(serviceClass)
entity Parent {
    name String required
}

@service(serviceClass)
entity Child {
    name String required
}

relationship OneToOne {
    Child{parent(name)} to @Id @OnDelete("CASCADE") Parent{child(name)}
}

, deleting a Parent with a Child in the admin UI isn't working: a pop-up panel confirms the deletion, but the data entry is still on the refreshed list. A similar JDL for the built-in User entity works, however.

@dto(mapstruct)
@service(serviceClass)
entity Address {
    street String
    city String
    state String
    zipCode String
}

relationship OneToOne {
     Address{user(login)} to @Id @OnDelete("CASCADE") User{address} with builtInEntity  
}

There is a related issue, the parent column is blank while parent data shows up in both view and edit pages.

Motivation for or Use Case

That is a basic entity data operation.

Reproduce the error

With the provided JDL, this bug can be reproduced.

Related issues
Suggest a Fix

In the Parent entity, add the following configuration

cascade = CascadeType.REMOVE, orphanRemoval = true

to

@OneToOne(fetch = FetchType.LAZY, mappedBy = "parent")
JHipster Version(s)

8.3

JHipster configuration

{ "generator-jhipster": { "applicationType": "monolith", "authenticationType": "jwt", "baseName": "oto", "buildTool": "gradle", "cacheProvider": "caffeine", "clientFramework": "vue", "clientTestFrameworks": [], "clientTheme": "none", "creationTimestamp": 1712685614957, "databaseType": "sql", "devDatabaseType": "h2Memory", "devServerPort": 9060, "enableGradleEnterprise": false, "enableHibernateCache": false, "enableSwaggerCodegen": false, "enableTranslation": false, "entities": ["Address", "Parent", "Child"], "feignClient": null, "gradleEnterpriseHost": null, "jhipsterVersion": "8.3.0", "jwtSecretKey": "...", "lastLiquibaseTimestamp": 1712686072000, "messageBroker": false, "microfrontend": null, "microfrontends": [], "nativeLanguage": "en", "packageName": "com.mycompany.myapp", "prodDatabaseType": "postgresql", "reactive": false, "searchEngine": false, "serverPort": null, "serverSideOptions": [], "serviceDiscoveryType": false, "syncUserWithIdp": null, "testFrameworks": [], "websocket": false, "withAdminUi": true } }


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

<!--
If the error is during an entity creation or associated with a specific entity.
If you are using JDL, please share that configuration as well.
-->

##### **Browsers and Operating System**

<!-- What OS are you on? is this a problem with all browsers or only IE8? -->

- [ ] Checking this box is mandatory (this is just to show you read everything)

<!-- Love JHipster? Please consider supporting our collective:
👉  https://opencollective.com/generator-jhipster/donate -->
OmarHawk commented 4 months ago
Suggest a Fix

In the Parent entity, add the following configuration

cascade = CascadeType.REMOVE, orphanRemoval = true

to

@OneToOne(fetch = FetchType.LAZY, mappedBy = "parent")

"normally" the @onDelete is something that's happening on database level. I wonder, why it isn't the case in this special situation.

github-actions[bot] commented 4 months ago

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

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.

vw98075 commented 4 months ago

For the User one-to-one case, the annotation @OnDelete("CASCADE") initiates a cascade operation within the DB schema. This approach, while functional, is somewhat of a workaround, likely stemming from the "immutable" constraint of the built-in entity User, rather than the standard procedure for cascade operations in JPA/Hibernate. If this annotation serves solely as a workaround, it should be clearly documented as such.

In the context of open source collaboration, it's essential to respect the design and functionality of dependent projects. While it's acceptable to propose changes or improvements, directly altering the behavior of another open-source project is generally discouraged.

Ahrovan commented 3 months ago

how to use orphanRemoval, dont work when add to "relationships": [ { "orphanRemoval": true, "otherEntityName": "listAttachment", "relationshipName": "attachments", "relationshipSide": "left", "relationshipType": "one-to-many" },

dont apply to after generation:
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "lists")
@JsonIgnoreProperties(value = { "lists" }, allowSetters = true)
private Set<ListAttachment> attachments = new HashSet<>();