wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes @OneToMany Cascading deletion problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "@OneToMany Cascading deletion problem" Watch "@OneToMany Cascading deletion problem" New topic
Author

@OneToMany Cascading deletion problem

Nitin Bhardwaj
Ranch Hand

Joined: Feb 02, 2007
Posts: 72
Hi Friends,

I am using JPA and the implementation is hibernate3.jar for this particular example.

I've two entities Product and Currency. Corresponding to them, there are two tables respectively:

Product
{
ProductId
ProductPrice
ProductName
ProductType
CurrencyId

}


Currency
{
Id
CurrencyCode
CurrencyName

}

The table Product has a field "CurrencyId" which is a Foreign key to the primary key "Id" of table Currency. Hence, the relationship is many to one as viewed from the side of Product entity.
The code snippet of Currency entity class is:


The code snippet of Product entity class is:


When I try to delete an entity of Currency class, it also deletes the corresponding records from Product table which refers to that Currency. For Ex: Suppose Product A, C, and D have the same currency say US Dollars. If I delete the Currency US Dollars then the Products A,C, and D are also deleted from the Product table. I just want that the CurrencyId of such products should be set to NULL inside the Product table instead of the Product records being deleted.
I've tried to use the following Cascade types also:
CascadeType.REMOVE: This has the same effect as CascadeType.ALL. The entities/records referring to the Currency being deleted are also deleted.
CascadeType.MERGE: An exception (org.hibernate.exception.ConstraintViolationException) is thrown which essentialy says that Foreign Key Constraint is violated..Can't delete record in Currency table as it is being referenced by records in Product table.
CascadeType.REFRESH: An exception (org.hibernate.exception.ConstraintViolationException) is thrown which essentialy says that Foreign Key Constraint is violated..Can't delete record in Currency table as it is being referenced by records in Product table.
CascadeType.PERSIST: An exception (org.hibernate.exception.ConstraintViolationException) is thrown which essentialy says that Foreign Key Constraint is violated..Can't delete record in Currency table as it is being referenced by records in Product table.

What should I specify so that when I delete the Currency entity the corresponding Product entities which refer to that Currency do not get deleted and their CurrencyId should simply be set to NULL in the PRODUCT table?

Thank you !
Nitin



Jonh Smith
Ranch Hand

Joined: Mar 18, 2010
Posts: 39
set the currency to null in all the products that have the currency to be deleted before you remove it.

you may drop the cascade remove if you never want products to be removed automatically when a currency is removed.

suggest you read this
Nitin Bhardwaj
Ranch Hand

Joined: Feb 02, 2007
Posts: 72
Thanks for the reply Jonh !
I removed the Cascade from @OneToMany annotation used in the Currency class. However, it still gives the Foreign Key Constraint violation exception.
For your second suggestion (to set the currency to null in all the products that have the currency to be deleted) I believe it will be a mechanical type of a step wherein I've to code this in a method. I was interested in changing the delete behaviour at Configuration level.
Thanks Again !
Nitin
Jonh Smith
Ranch Hand

Joined: Mar 18, 2010
Posts: 39
"For your second suggestion "

Unfortunately it is not a suggestion :) It's a requirement. You have to do it. No shortcut in jpa. maybe some jpa provider has some solution i'm not aware of.

List<Product> products = currency.getProducts();
for(Product p : products)
{
p.setCurrency(null);
}
em.remove(currency);



Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 518
    
    2

Hi All,

I do not intend to hijack this thread with my naive question, but I'm just curious to know, if Hibernate 3 is a JPA implementation. My understanding was JPA was built on the concepts from different ORM like hibernate , Toplink etc.

Again, this is absolutely an irrelevant question to this thread, but just thought of clarifying my question.


Regards
KumarRaja

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

Kumar Raja wrote: I do not intend to hijack this thread with my naive question, .

For the future, it is ok to start a new thread for a new question.

Kumar Raja wrote: but I'm just curious to know, if Hibernate 3 is a JPA implementation. My understanding was JPA was built on the concepts from different ORM like hibernate , Toplink etc.

Hibernate predated JPA. Many of the ideas in JPA came from Hibernate. After JPA came out, Hibernate added APIs matching the names used in JPA. Now Hibernate allows you to use the original or JPA method names.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 518
    
    2

Thank you and I apologize for using this thread, instead of creating a new one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: @OneToMany Cascading deletion problem
 
Similar Threads
Foreign key must have same number of columns as the referenced primary key
automatically delete or insert
Stuck on manyToMany association
OneToMany Unidirectional strange behavior ?
Deleting data from multiple tables?