SteelReed

Search

Search posts, questions…

JPA & HibernateQuestion 9 / 24

Junior

What is the owning side of a JPA relationship, and what is mappedBy for?

Short answer

The side holding the foreign key is the owning side (usually @ManyToOne); only that side is written to the DB. mappedBy marks the other side.

ignored when writingwrites the foreign keyOrder@OneToMany mappedByorder_item tableorder_id columnOrderItem@ManyToOne

3 points to say:

  1. The foreign key is in the "many" table; that side owns it.
  2. mappedBy goes on the side without the foreign key.
  3. Write a helper method that sets both sides.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.