File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes how hibernate maintain referential integrity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "how hibernate maintain referential integrity " Watch "how hibernate maintain referential integrity " New topic

how hibernate maintain referential integrity

IshwarJava Chand

Joined: Jul 04, 2007
Posts: 3
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

Joined: Apr 14, 2004
Posts: 10336

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.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
IshwarJava Chand

Joined: Jul 04, 2007
Posts: 3
Hi Paul,

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

Tejas Aj

Joined: Feb 10, 2010
Posts: 6
can you try putting a relationship(pk - fk) between the employee and address table and then try.

I agree. Here's the link:
subject: how hibernate maintain referential integrity
It's not a secret anymore!