aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "how hibernate maintain referential integrity " Watch "how hibernate maintain referential integrity " New topic
Author

how hibernate maintain referential integrity

IshwarJava Chand
Greenhorn

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
Bartender

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
Greenhorn

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
Greenhorn

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

Regards,
Tejas.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how hibernate maintain referential integrity
 
Similar Threads
Deleting Record in a Web Application.
Problem in <one-to-one> relation mapping
Batch Delete Problem
Mapping Error Hibernate
One to One mapping in Hibernate 3