Sub topic 6
Modeling associations and the owning side
This chapter assumes you know core Java, basic SQL (primary keys, foreign keys, joins), and how to map a class with @Entity and @Id. It covers how JPA maps relationships between entities and which side controls the database column.
Objects hold references, tables hold keys
In Java, a Book holds a reference to an Author, and an Author can hold a list of Book objects. In SQL, only one table stores the link: books.author_id is a foreign key to authors.id. JPA must decide which Java field writes that column. That field is the owning side of the association. The other side is the inverse side. It is read-only as far as the database is concerned.
Many-to-one: the owning side
The Book entity owns the foreign key. @JoinColumn names the column.
import jakarta.persistence.*;
@Entity
@Table(name = "books")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String title;
private int publishedYear;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "author_id")
private Author author;
protected Book() {
}
public Book(String title, int publishedYear) {
this.title = title;
this.publishedYear = publishedYear;
}
public Long getId() { return id; }
public String getTitle() { return title; }
public int getPublishedYear() { return publishedYear; }
public Author getAuthor() { return author; }
void setAuthor(Author author) { this.author = author; }
}@ManyToOne defaults to eager fetching, so the example sets LAZY explicitly. optional = false makes the column NOT NULL.
One-to-many: the inverse side
Author declares the list with mappedBy = "author". The value is the field name on Book, not the column name.
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name = "authors")
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 100)
private String name;
@OneToMany(mappedBy = "author", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Book> books = new ArrayList<>();
protected Author() {
}
public Author(String name) {
this.name = name;
}
public void addBook(Book book) {
books.add(book);
book.setAuthor(this);
}
public Long getId() { return id; }
public String getName() { return name; }
public List<Book> getBooks() { return books; }
}Two options on the collection matter:
cascade = CascadeType.ALLpasses persist, merge and remove operations from the author to its books.orphanRemoval = truedeletes a book when it is removed from thebookslist.
Keep both sides in sync
JPA reads only the owning side when it writes. If you call author.getBooks().add(book) and never set book.author, the foreign key is saved as null, and the insert fails here because of optional = false. The addBook helper updates both sides in one call. Always use it instead of touching the list directly.
Other association types
| Annotation | Owning side | Notes |
|---|---|---|
| `@ManyToOne` | The class with the annotation | Foreign key column lives in its table. |
| `@OneToMany(mappedBy = ...)` | The other class | Inverse side. Without `mappedBy`, JPA creates a join table. |
| `@OneToOne` | The side with `@JoinColumn` | Put `mappedBy` on the other side. |
| `@ManyToMany` | The side without `mappedBy` | Uses a join table. Prefer `Set` over `List`. |
Common errors
- Omitting
mappedByon a bidirectional@OneToMany. JPA then creates an unwanted join table. - Setting only the inverse side, which leaves the foreign key null.
- Using
CascadeType.REMOVEon@ManyToMany, which can delete shared rows.
Exercise
In a bidirectional Author and Book mapping, you run author.getBooks().add(book) without calling book.setAuthor(author), then save the author. What is written to books.author_id, and why?
Show answer
Null (and the insert fails with optional = false). Book.author is the owning side, and JPA ignores changes made only to the inverse books list when it writes the foreign key.