-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Cascade delete not working for @OneToMany mapping
PostPosted: Mon Sep 14, 2009 4:12 am 
Newbie

Joined: Mon Sep 14, 2009 3:56 am
Posts: 2
Hi All,

I am new to Hibernate using Hibernate3 with annotations. I have a very wierd problem in my code. I am using @OneToMany and @ManyToOne mapping between 2 classes. The logic is that one Collection can have many Files and Many Files can belong to one Collection. The code is like this:

Code:
In class Collection:

@OneToMany (cascade=CascadeType.REMOVE, mappedBy="dataCollection")
     private List<CollectionFilesMapping> filesList;


In class File:
@ManyToOne(targetEntity = DataCollection.class)
@JoinColumn (name="Collection_ID")
   private DataCollection dataCollection;




But the Cascade simply does not seem to work :(
I tried out a number of changes in the properties but in vain.
I get an exception like this:

Cannot delete or update a parent row: a foreign key constraint fails (`files`, CONSTRAINT `FK263465956066C15` FOREIGN KEY (`Collection_ID`) REFERENCES `collection` (`Collection_ID`))

I have other @ManyToMany mapping in this class Collection and that works fine.
Any help would be greatly appreaciated!

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Cascade delete not working for @OneToMany mapping
PostPosted: Mon Sep 14, 2009 4:56 am 
Beginner
Beginner

Joined: Wed Jun 17, 2009 9:03 pm
Posts: 31
Location: mumbai
Hi although i dont have good idea about annotations. Check if this works

for parent
Code:

@OneToMany(mappedBy = "parent", cascade = {javax.persistence.CascadeType.ALL})
@Cascade({org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
@JoinColumn(name = "parent_id")
private List<Child> children = new ArrayList<Child>();



and for child
Code:

@ManyToOne
@OnDelete(action = org.hibernate.annotations.OnDeleteAction.CASCADE)
@JoinColumn(name = "parent_id")
private Parent parent;


Top
 Profile  
 
 Post subject: Re: Cascade delete not working for @OneToMany mapping
PostPosted: Mon Sep 14, 2009 5:18 am 
Newbie

Joined: Mon Sep 14, 2009 3:56 am
Posts: 2
Thanks for your reply. I tried out what you suggested but that did not work... I still get the same error :(
Can you think of any other options?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.