开发者

Hibernate 3 On delete cascade

开发者 https://www.devze.com 2023-04-11 11:32 出处:网络
I have a many-to-on开发者_如何学Goe mapping on bookings. A booking must belong to a room. And a room can have several bookings.

I have a many-to-on开发者_如何学Goe 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;
...
}


In your Room entity you can have a

@OneToMany(cascade=CascadeType.REMOVE) 
private List<Booking> bookings;


Use

 @ManyToOne(cascade = CascadeType.REMOVE)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号