Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

update child foreign column when parent is deleted

 
ankur jadiya
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my requirement is when i delete the parent ,child should not be deleted but instead of deleting child,one of the child column department_ID which is foreign key must be update with some value , say one department(Parent Table) contains many employee and employee(child Table) table have Department_ID as a foreign key column .The problem i m facing when i delete the parent table nothing is reflected on child cause i have make updatable=false if i will make it true then on deleting parent ,child column is set to null or if i update the parent then also child Department_Id column set to null which is violating the requirement so that's why i make it false .I don't want child to be update when the parent make any changes only changes should reflected on delete operation ,only on one column. Please help me what should i do? thanks

This is my Praent Table(Department code)


This is child table Employee code
 
T Mishra
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure if this will work but you can try the following approach and let us know.
when i delete the parent ,child should not be deleted

@OneToMany(fetch = FetchType.EAGER, orphanRemoval=false)

use of updatable=false looks correct to me
i delete the parent ....one of the child column department_ID which is foreign key must be update with some value

This seems to be specific to Delete Event. I assume when relationship is deleted, in the entity Department, Collection<Employee> employeeList is set to null

You may define an even listener for delete action here. More description about event here
Implement PreCollectionRemoveEventListener and add it to your Configuration

In the listener method onPreRemoveCollection(PreCollectionRemoveEvent event)

1. create PreCollectionRemoveEvent pass the collection employeeList in constructor,
2. for each Employee in Collection<Employee> employeeList
set DEPARTMENT_ID = <default_value>
save employee

If PreCollectionRemoveEvent doesn't work, you may try PreCollectionUpdateEvent
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic