-->
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.  [ 1 post ] 
Author Message
 Post subject: Does @Cascade(CacadeType.DELETE) work with @ManyToOne?
PostPosted: Wed Jan 09, 2008 10:50 am 
Newbie

Joined: Wed Jan 09, 2008 10:04 am
Posts: 2
Hibernate version:
Core 3.2.5, Annotations 3.3.0

I have a simple pair of parent and child classes where the child has a reference to the parent in a @ManyToOne relationship as show below. I implemented a Cascade Delete using the @OnDelete(OnDeleteCascade.DELETE) annotation and it works fine.

I would like to be able to deploy this change to older installations where this annotation did not exist and thus the database DDL was missing the ON DELETE CASCADE constraint when it was created. I would like to avoid deploying upgrade scripts or procedures to the database.

So I tried getting the Hibernate-level cascade delete to work with @Cascade(CascadeType.DELETE) assuming this would be the answer (correct?). I'm not sure if I am doing something wrong or if the @Cascade is not properly supported for @ManyToOne relationships but I could not get it to work.

Here's an extract of my current code:

Child

@Entity
@Table(name="mychild")
public class MyChild implements Serializable {

private int id;
private MyParent parent;

@ManyToOne
@JoinColumn(name = "parent_id")
@OnDelete(action = OnDeleteAction.CASCADE)
public MyParent getParent() {
return parent;
}
}

Parent

@Entity
@Table(name="myparent")
public class MyParent implements Serializable {

private int id;
private String parentName;

}


Among many other things, I tried replacing @OnDelete(action = OnDeleteAction.CASCADE) with @Cascade(CascadeType.DELETE) and calling session.delete(myparent) but it kept failing with a "foreign constraint exception" in MySQL.

What am I missing?

Thanks,
Ralph


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

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.