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-singleassocQuote:
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.htmlQuote:
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?