Use mappedBy on the inverse side of a bidirectional Jakarta Persistence association to name the property that owns the relational mapping. For an Item–Bid association, Bid#item owns the ITEM_ID foreign key, while Item#bids declares @OneToMany(mappedBy = "item"). Hibernate then loads the collection through the foreign key already mapped by Bid#item instead of creating another relationship mapping. section 8.3.2
Bidirectional one-to-many mapping
@Entity public class Item { @OneToMany(mappedBy = "item", fetch = FetchType.LAZY) private Set<Bid> bids = new HashSet<>(); } @Entity public class Bid { @ManyToOne @JoinColumn(name = "ITEM_ID") private Item item; } The value "item" is the Java property on the other entity, not a database column name. The collection defaults to lazy fetching, so bids load when the collection is accessed. section 8.3.2
Apply these ownership rules when choosing the mappedBy side:
- Place mappedBy on the collection or other inverse side when the opposite property already maps the foreign key. section 8.3.2
- Do not put mappedBy on @ManyToOne; that annotation is always the owning side in this association pattern. section 9.2.2
