Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how hibernate maintain referential integrity

 
IshwarJava Chand
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hi Paul,

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

 
Tejas Aj
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic