liveBook · Manning

mappedBy

As explained in Java Persistence with Spring Data and Hibernate epub

mappedBy is a Jakarta Persistence association-mapping attribute that names the property owning the relational mapping on the other side of a bidirectional association.

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

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains property naming, ownership, fetching, synchronization, and ordered-list consequences, but defers broader association design.
Examples
Uses Item–Bid collections, a User–Address one-to-one association, and an ordered Bid list with a duplicated join column.
What it assumes
Assumes Java entity annotations and basic associations, while clarifying ownership, property names, and bidirectional synchronization.
What you can do after
Apply it via Jakarta Persistence annotations to declare inverse ownership and avoid conflicting foreign-key mappings.
How it connects
Connects bidirectional associations, @OneToMany, @ManyToOne, foreign keys, lazy loading, cascading, and @OrderColumn.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.