-->
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: Cascades with MySql
PostPosted: Mon Aug 14, 2006 9:59 am 
Newbie

Joined: Mon Aug 14, 2006 9:31 am
Posts: 3
Hibernate version: 3.1.3

Name and version of the database you are using: MySql 5.0


Hello,

I'm new to hibernate, so far everything works fine - except for one detail: i can't get on delete cascade to work.

i'm using hibernate annotations.

my entities look like this:
Code:
@Entity
@Name("address")
public class Address implements Serializable {

   @Id @GeneratedValue
   @NotNull
   public Long getId() {
      return id;
   }

       //more fields....

   @OneToMany(cascade = {CascadeType.ALL })
    @Cascade(value = {org.hibernate.annotations.CascadeType.ALL,
                    org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @JoinColumn(name = "address")    
   public Set<Communication> getCommunication() {
      return communication;
   }


}


@Entity
@Name("communication")
public class Communication {

   @Id @GeneratedValue
   @NotNull
   public Long getId() {
      return id;
   }

        //more fields....
   @ManyToOne()
        @JoinColumn(name="address", insertable=false, updatable=false)
   public Address getAddress() {
      return address;
   }

}


everything works fine, except the sql statement is missing the "on delete cascade".
so when i try to delete an address, the corresponding communications are not delete but instead an error is raised...

What can I do?

thank you very much!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 17, 2006 8:03 am 
Newbie

Joined: Mon Aug 14, 2006 9:31 am
Posts: 3
Isn't there anyone out there, who can help me with this?
I thought this to be a beginner question? I searched the forum back and forth but couldn't find anything that helped me.

Maybe my question is too awkward:
I'm just searching for a way in hibernate annotations to describe a one-to-many reationship, where a delete will be cascaded.

example:
I have a contact with many addresses. Now, if I delete the contact, the addresses are supposed to be deleted too, automatically.

I would expect that the "CascadeType.DELETE_ORPHAN" would generate a foreign key constraint with on delete cascade, but somehow it doesn't.
Isn't that the way to go?

What can i do? Thank you very much!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 17, 2006 12:51 pm 
Newbie

Joined: Mon Aug 14, 2006 9:31 am
Posts: 3
i got one step further, in case someone is interested:

1. you have to use the MySQLInnoDBDialect, the MySQLDialect won't work.
2. the right annotation is @OnDelete(action=OnDeleteAction.CASCADE) from the Annotation Extensions.

now, this works, when I declare a unidirectional @ManyToOne association. but with a Bidirectional ManyToOne association, it won't.

can someone help?


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.