-->
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: Function of @OnDelete annotation
PostPosted: Fri Jul 03, 2009 9:18 pm 
Beginner
Beginner

Joined: Mon Jul 07, 2008 8:07 pm
Posts: 42
How does the org.hibernate.annotations.OnDelete annotation work? The documentation seems pretty sparse on it:
from http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-hibspec-singleassoc
Quote:
Sometimes you want to delegate to your database the deletion of cascade when a given entity is deleted.
Code:
@Entity
public class Child {
    ...
    @ManyToOne
    @OnDelete(action=OnDeleteAction.CASCADE)
    public Parent getParent() { ... }
    ...
}

In this case Hibernate generates a cascade delete constraint at the database level.


from http://docs.jboss.org/hibernate/stable/annotations/api/org/hibernate/annotations/OnDelete.html
Quote:
Strategy to use on collections, arrays and on joined subclasses delete OnDelete of secondary tables currently not supported.


So what exactly will it do? Basically, I have a relationship like this:

A User object is referenced by a Friend object via a many-to-one relationship. The actual User object does not have reference to the Friend object, thus it is a unidirectional Many-To-One association.

Code:
@Entity
public class User {
    ...
}

@Entity
public class Friend {
    @ManyToOne(targetEntity=User.class, fetch=FetchType.EAGER)
    @JoinTable(name="user_friends", joinColumns=@JoinColumn(name="friend_id"), inverseJoinColumns=@JoinColumn(name="user_id"))
    private User friendReference;
    ...
}


What I want is that when the User object (which is referenced by the Friend object as seen above) is deleted, the Friend object which references it is also deleted. Will the @OnDelete object delete the Friend object when the User object is deleted? Or is it the other way around?


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.