Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate 3 On delete cascade

I have a many-to-one mapping on bookings. A booking must belong to a room. And a room can have several bookings.

If a room is deleted, I would like all the bookings on that room to be deleted as well. How would i go about doing this using hibernate annotations?

@Entity
public class Booking implements Serializable{

    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private Date startDate;
    private Date endDate;
    private Date createdDate;

    @ManyToOne
    @JoinColumn (name = "roomId")
    private Room room;
...
}
like image 789
user829237 Avatar asked Oct 05 '11 12:10

user829237


2 Answers

In your Room entity you can have a

@OneToMany(cascade=CascadeType.REMOVE) 
private List<Booking> bookings;
like image 158
Bozho Avatar answered Oct 05 '22 03:10

Bozho


Use

 @ManyToOne(cascade = CascadeType.REMOVE)
like image 29
danny.lesnik Avatar answered Oct 05 '22 02:10

danny.lesnik