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

Cannot delete recipe after you update it #135

Open
TimberBro opened this issue Mar 31, 2022 · 1 comment
Open

Cannot delete recipe after you update it #135

TimberBro opened this issue Mar 31, 2022 · 1 comment

Comments

@TimberBro
Copy link

Steps to reproduce:

  1. Start application.
  2. Navigate to http://localhost:8080/recipe/1/update to update the recipe.
  3. Change nothing and click the "Submit" button.
  4. Navigate to http://localhost:8080/recipe/1/delete to delete the updated recipe.
  5. Get the following error:

could not execute statement; SQL [n/a]; constraint ["FKDBFSIV21OCSBT63SD6FG0T3C8: PUBLIC.NOTES FOREIGN KEY(RECIPE_ID) REFERENCES PUBLIC.RECIPE(ID) (1)"; SQL statement: delete from recipe where id=? [23503-197]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

Expected behavior:
You can delete the recipe after an update.

@TimberBro
Copy link
Author

The issue is in entity modeling.
After updating the recipe the new row is created in the "notes" table. The new note has reference to the recipe_id.
On the other side, the "recipe" table stores reference only for the new Note.

Possible solution:
Add orphanRemoval to the relationship. This parameter will prevent creation of orphaned records and will fix initial issue.
Here's a code snippet for Recipe.java:

    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)  
    private Notes notes;

mfearby added a commit to mfearby/spring5-recipe-app that referenced this issue Jul 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant