• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

[Spring Boot] Many To One relationship continues after deleting the object

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm developing a backend application with Spring Boot.
In this backend I have a Many To One relationship between two tables, the Organization table (One, Owner of the relationship) and the Asset table (Many).
Everything works fine, I can add new objects and they are related correctly.
But when I try to delete child objects (Asset) it just leaves the ID field of the Organization table as null and the Asset object continues to exist in the Database.

This is the part of the database where the tables are created:


Object Asset:


Object Organization:


Delete endpoint:


Database photo with object before deletion


Database photo with object after deletion


Before trying this way of deleting, I tried to remove the object from the organization object list, but I got a SQLGrammarException on the console.


Error:

 
Bartender
Posts: 210
14
Mac OS X IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not deleting it, you are just removing the reference to organization. That's probably not what you want.
Why not call delete method in assetRepository?
 
Saloon Keeper
Posts: 29001
214
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a JPA problem, not influenced by the fact that what you are doing in a webapp or Spring Boot, so I'm moving the question to our ORM forum.
 
Tim Holloway
Saloon Keeper
Posts: 29001
214
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note to the easily-confused (e.g., me): @DeleteMapping has nothing to do with databases. It's an annotation that maps an HTTP DELETE verb to the annotated method (as opposed to a GET or POST).

In JPA, you cannot unilaterally connect and disconnect relationships reliably. If you're going to delete an Asset and it's related to other objects, then those other objects must be updated to remove references to the Asset Entity that you are deleting. And you must include the affected relating Entities in the collection of Entities to be updated in the database. Otherwise, yes, strange things can happen.

In the case of a parent/child with cascading, the cascade will delete ALL children when you delete the parent, but deleting a single child without updating the parent won't work.

Also note that in the event that you're doing lazy-fetch with detaching, you'll need to re-attach (merge) anything that wasn't fetched before detaching.
 
Matheus Markies
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Note to the easily-confused (e.g., me): @DeleteMapping has nothing to do with databases. It's an annotation that maps an HTTP DELETE verb to the annotated method (as opposed to a GET or POST).

In JPA, you cannot unilaterally connect and disconnect relationships reliably. If you're going to delete an Asset and it's related to other objects, then those other objects must be updated to remove references to the Asset Entity that you are deleting. And you must include the affected relating Entities in the collection of Entities to be updated in the database. Otherwise, yes, strange things can happen.

In the case of a parent/child with cascading, the cascade will delete ALL children when you delete the parent, but deleting a single child without updating the parent won't work.

Also note that in the event that you're doing lazy-fetch with detaching, you'll need to re-attach (merge) anything that wasn't fetched before detaching.



When I try to put a repository.delete I get a SQLGrammarException:

Exception: InvalidDataAccessResourceUsageException - could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Stack trace:

assetRepository.deleteById(asset.getId());
 
Tim Holloway
Saloon Keeper
Posts: 29001
214
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get a SQL dump to the logs. The application.yaml I use looks like this:

For application.properties that translates as the more verbose:


Except that you want to trace on org.hibernate.sql or something like that.

 
Matheus Markies
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:You can get a SQL dump to the logs. The application.yaml I use looks like this:

For application.properties that translates as the more verbose:


Except that you want to trace on org.hibernate.sql or something like that.



Thank you very much!
Doing the logs I found that the error is not in the relationship between Organization and Asset, but in another relationship that involved the Asset, I fixed it and managed to delete correctly!
 
Bartender
Posts: 2466
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matheus,
You may want to try CrudRepository or JpaRepository interface in Spring Data JPA to delete your Assets.
Reference:
https://www.baeldung.com/spring-data-jpa-delete
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic