środa, 24 lutego 2016

Hibernate - relation owner explained

Implementing bi-directional relationship is fairly easy whilst not very obvious for just off the boat hibernate users. Bi-directional relationship means you can navigate from one entity to the another and vice-versa. To do this you need mappedBy property. Lets say we have the following model.
  BrickSet owns many Bricks
  One Brick belongs to many BrickSet


@Entity
@Table(name = "BrickSet")
public class BrickSet {
  
@ManyToMany(fetch=FetchType.Lazy, cascade= {CascadeType.ALL}
@JoinColumn(name="id")
public Set getBricks() {
  return bricks;
}

}

Brak komentarzy:

Prześlij komentarz