• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Delete Bidirectional many to one Entities

 
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am new to Hibernate and I am currently working on it. I have 2 tables an employee and a person table. The tables are related as shown below:

Employee.java



Person.java



Since orphanRemoval is true, I know that if a person record is deleted, the employee record will be deleted. But what will happen if I delete a employee record?

When I try to delete an employee record nothing is happening. There are no errors no exceptions and the employee record also doesn't remove. What I am trying to do here is, if I delete a Employee record, the concerned Person record should also be deleted. I don't have the option of deleting person record, so I am deleting employee record. I am breaking my head over this issue for past 3 days Please help me.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your Person has many Employees.
If that is not a modelling error (should it be a One-to-One?), then why should the removal of a single Employee out of a possible several imply the removal of the Person as a whole (including all other Employee rows associated with it)?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Partheban Udayakumar wrote:What I am trying to do here is, if I delete a Employee record, the concerned Person record should also be deleted.


That makes no sense at all! Because your Person has a set of Employee.

So if I have 2 part-time jobs, you'll have 1 Person object having 2 Employee objects. If I quit one of my part-time job, that Employee object will be removed and I (the Person object) will be removed as well together with my other part-time job. Sounds like your current model doesn't make any sense at all! And if your model is incorrect, it could be very hard to get some things done (as you are currently experiencing).
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave and Roel,

Thanks for your reply. I discussed with my senior regarding what you said in your answers and yes I misinterpreted it. If you remove an Employee object only that object should be removed but I am not able to do that too. The person foreign key can be null in the table. I am using Postgresql as database. Waiting for your replies.
My Employee table



Parthe
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you have a bidirectional association, it's your responsibility to make the changes at both sides to ensure that your object model is consistent.

Meaning you Person class should have a removeEmployee method like this
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roel,

I am using angular js and spring data jpa. I would use angular js to call the repository methods.

controller.js



Do you have an idea of how to set the value null using angular js?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume your front-end (angular) communicates with the back-end (e.g. a Spring MVC controller). And this controller invokes a business service method (e.g. removeEmployee(id)). And after this business method executes the required business logic (and maybe even some validation), invokes the appropriate spring data jpa repository method to save the person (and delete the employee).
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roel,

Yes. What you have assumed is correct but there is no user defined method (as removeEmployee(id)). It invokes the repository's delete method repo.delete(id). I have my repo class extend JPA repository. Here are the files

UserServiceImpl.java



UserRepository.java


 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Partheban Udayakumar wrote:It invokes the repository's delete method repo.delete(id). I have my repo class extend JPA repository. Here are the files


In the code you provided, I don't see any delete method being invocated.

Off topic: it's strange that a getPassword method returns an Employee object.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, does the Employee get deleted?
If so, what do you then do?

What is it that's going wrong?

The Employee row will no longer be in the database, so cannot be returned when you select the Person.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roel,
You would have noticed that my repo class extends jpa repository. Jpa repository has a predefined delete function. I haven't overriden it. I'm using the same.

Reply to off-topic:Code I got from my seniors. So I will have to analyze it. We are rushing through to complete the project.

Dave,
Nothing happens to employee object that is the problem.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Partheban Udayakumar wrote:You would have noticed that my repo class extends jpa repository. Jpa repository has a predefined delete function. I haven't overriden it. I'm using the same.


I know JpaRepository defines a delete method. That was not my question. I want to know where and how the repository's delete method is invoked with the parameter you got from the client. I can't imagine the angular code directly invokes the repository's delete method. That would be a very bad, bad practice!

Partheban Udayakumar wrote:Nothing happens to employee object that is the problem.


That's probably because (as already mentioned in a previous post) when you have a bidirectional association, it's your responsibility to make the changes at both sides to ensure that your object model is consistent. So you can't delete an Employee by just the id. You have to get the Employee object from database, set its Person instance member to null and remove the Employee object from the set of employees of that Person object. If you want to delete an Employee by only its id, you should use a unidirectional association.

But it's your lucky day! One of our applications has exactly the same model hierarchy (including the bidirectional association). And one of the requirements is also to delete a child (from the parent's collection). So that's almost exactly the same as your application, only our application works with Dossier (= Person) and Lijn (= Employee) objects. And our application works flawlessly for more than 1 year (and counting). I'll post only the relevant code (model, repository, service,...), so you can easily compare with your code and adjust accordingly.



Hope it helps!
Kind regards,
Roel
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roel,

You made my day. Your example worked like a gem for me. In your DossierServiceImpl,java I had to add code for deleting Lijn. Kindly check that else everything was perfect for me. Please edit that so in future if someone refers this post it would be helpful for them.

Thank You very much

Regards
Parthe
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Partheban Udayakumar wrote:In your DossierServiceImpl,java I had to add code for deleting Lijn.


Which code did you have to add? Because the DossierServiceImpl class has a deleteLijn method which takes care of deleting a Lijn.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am outside now. Once I get my system I will post it. Meanwhile can you see this issue too.
Save bidirectional foreign key
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roel,

I have added



after dossierRepository.save(entity);

If I am not including it only parent value in lijn is deleted once i use predefined delete method from repository it deletes the record in the table too.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I already stated in a previous post, the code I posted works flawlessly. So the (orphan) Lijn record will be deleted from database by only saving the Dossier instance. That's why you have the orphanRemoval property set to true. More info about orphan removal in relationships can be found here.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roel,

Ya ok. I get it now
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic