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

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

Open
vw98075 opened this issue Apr 9, 2024 · 4 comments

Comments

@vw98075
Copy link
Contributor

vw98075 commented Apr 9, 2024

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
Copy link
Contributor

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.

Copy link
Contributor

github-actions bot commented Apr 11, 2024

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
Copy link
Contributor Author

vw98075 commented Apr 11, 2024

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
Copy link

Ahrovan commented Apr 30, 2024

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<>();

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

4 participants