Hibernate: One-to-Many Bidirectional One-Parent

I am trying to establish a one-to-many bidirectional relationship with "one" as the parent

I have a parent:

@Entity public class VideoOnDemand { @OneToMany(cascade = CascadeType.ALL) @LazyCollection(LazyCollectionOption.FALSE) @JoinColumn(name = "video_id") private List<CuePoint> cuePoints = new ArrayList<CuePoint>(); } 

and child:

 @Entity public class CuePoint { @ManyToOne(cascade=CascadeType.ALL) @JoinColumn(name = "video_id", insertable = false, updatable = false) private VideoOnDemand video; } 

I used the recommendations of the official Hibernate documentation (2.2.5.3.1.1). However, Hibernate does not seem to understand that CuePoint is a child, so when I remove CuePoint, it also removes VideoOnDemand with all the other CuePoints.

What am I doing wrong and right?

+4
source share
1 answer

Thus, you have created a unique bidirectional connection as two unidirectional associations. One of the sides should be marked as the opposite of the other:

 @Entity public class VideoOnDemand { @OneToMany(mappedBy = "video", cascade = CascadeType.ALL) private List<CuePoint> cuePoints = new ArrayList<CuePoint>(); } @Entity public class CuePoint { @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name = "video_id", insertable = false, updatable = false) private VideoOnDemand video; } 

The mappedBy attribute must contain the attribute name of the other side of the association.

Please note that this is indeed what is described in clause 2.2.5.3.1.1. documentation.

+6
source

All Articles