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

how hibernate maintain referential integrity

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following scenario:

Many employees can share the same address.

For which i have the following POJO classes:

1. Employee class that has properties name, designation of String type and address of Address type.
2. Address class that has properties homeNum, sector and block of String type.

The below is the Employee and Address POJO class:




The below is the employee.hbm.xml file:


Some part of application code:



From the above code ishwar and abhijeet both share the same address. In this case there are 2 rows get added in the Employee table
and 1 row added in the Address table.

My question is that when I delete a employee using session.delete(employee) from the appliaction code. then from the Employee table a record for that employee get deleted and also its address entries from the Address table too get deleted. while another employee still has a reference of that address in the Employee table.

How can i prevent the address record not to get deleted from the Address table , if some employees have references of it in Employee table, when an employee is deleted.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have defined your employee - address association to use a cascade delete. If addresses are not child objects of employees you probably don't want to define them this way.
 
IshwarJava Chand
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Thanks for the reply.
Can you suggest me then what is the best way to define it.

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
can you try putting a relationship(pk - fk) between the employee and address table and then try.

Regards,
Tejas.
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic